Skip to content

Incorrect behavior of the transaction method #3328

Open
@babaduk47

Description

@babaduk47
  • Laravel-mongodb Version: 5.2.0
  • PHP Version: 8.3.17
  • Database Driver & Version: 1.21.1

Description:

The transaction method of the mongodb connection does not call the beginTransaction method. If dispatch job with afterCommit=true, the queue will be sent before commit. This behavior differs from mysql connection

Steps to reproduce

try {
    DB::connection('mysql')->transaction(static function (): void {
        dispatch(fn() => dump('dump from mysql dispatch'))
            ->onConnection('sync')
            ->afterCommit();

        throw new \Exception('Command not executed.');
    });
} catch (\Exception) {
}

try {
    DB::connection('mongodb')->transaction(static function (): void {
        dispatch(fn() => dump('dump from mongodb dispatch'))
            ->onConnection('sync')
            ->afterCommit();

        throw new \Exception('Command not executed.');
    });
} catch (\Exception) {
}

Expected behaviour

Jobs will not be completed

Actual behaviour

We get dump = dump from mongodb dispatch

Additional debug

DB::connection('mysql')->transaction(static function (): void {
    $connection = DB::connection('mysql');

    $transactionsManager = (fn () => $this->transactionsManager)->bindTo($connection, $connection)();
    $pendingTransactions = (fn () => $this->pendingTransactions->all())->bindTo($transactionsManager, $transactionsManager)();

    dump('mysql transactionLevel: ' . DB::connection('mysql')->transactionLevel());
    dump('mysql pendingTransactions: ', $pendingTransactions);
});

DB::connection('mongodb')->transaction(static function (): void {
    $connection = DB::connection('mongodb');

    $transactionsManager = (fn () => $this->transactionsManager)->bindTo($connection, $connection)();
    $pendingTransactions = (fn () => $this->pendingTransactions->all())->bindTo($transactionsManager, $transactionsManager)();

    dump('mongodb transactionLevel: ' . DB::connection('mongodb')->transactionLevel());
    dump('mongodb pendingTransactions: ', $pendingTransactions);
});
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions