laravel-mongodb - Issue #3328: Incorrect behavior of the transaction method

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • None
    • None
    • None
    • None
    • None
    • None

      babaduk47 has created Issue #3328: Incorrect behavior of the transaction method in laravel-mongodb. This Jira ticket was filed by GromNaN

      Issue Text:

      • 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);
      });
      

      <img width="741" alt="Image" src="https://github.com/user-attachments/assets/e0ce298c-2092-4318-ac06-e25ab4508ff2" />

              Assignee:
              Pauline Vos
              Reporter:
              TPM Jira Automations Bot
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: