Uploaded image for project: 'PHP ORMs'
  1. PHP ORMs
  2. PHPORM-188

laravel-mongodb - Issue #2851: Transaction level not working when use closure

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Laravel

      florianJacques has created Issue #2851: Transaction level not working when use closure in laravel-mongodb. This Jira ticket was filed by GromNaN

      Issue Text:
      The transaction level remains at 0 when we use laravel automatic transactions.

      ```php
      $connection->transaction(function () use ($connection)

      { dump($connection->transactionLevel()); }

      );
      ```
      Display 0.

      Here's a suggested fix

      ```php
      /**

      • Static transaction function realize the with_transaction functionality provided by MongoDB.
        *
      • @param int $attempts
        */
        public function transaction(Closure $callback, $attempts = 1, array $options = []): mixed
        {
        $attemptsLeft = $attempts;
        $callbackResult = null;
        $throwable = null;

      $callbackFunction = function (Session $session) use ($callback, &$attemptsLeft, &$callbackResult, &$throwable) {
      $attemptsLeft--;

      $this->transactions = 1;

      if ($attemptsLeft < 0)

      { $session->abortTransaction(); $this->transactions = 0; return; }

      // Catch, store, and re-throw any exception thrown during execution
      // of the callable. The last exception is re-thrown if the transaction
      // was aborted because the number of callback attempts has been exceeded.
      try

      { $callbackResult = $callback($this); }

      catch (Throwable $throwable)

      { throw $throwable; }

      finally

      { $this->transactions = 0; }

      };

      with_transaction($this->getSessionOrCreate(), $callbackFunction, $options);

      if ($attemptsLeft < 0 && $throwable)

      { throw $throwable; }

      return $callbackResult;
      }
      ```

            Assignee:
            Unassigned Unassigned
            Reporter:
            dbeng-pm-bot PM Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: