Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect behavior of the transaction method #3328

Open
babaduk47 opened this issue Mar 27, 2025 · 0 comments
Open

Incorrect behavior of the transaction method #3328

babaduk47 opened this issue Mar 27, 2025 · 0 comments

Comments

@babaduk47
Copy link

babaduk47 commented Mar 27, 2025

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant