mongodb/laravel-mongodb · error · RuntimeException

There is no active session.

Error message

There is no active session.

What it means

ManagesTransactions::commit() and rollBack() require an active MongoDB client session (started via beginTransaction). getSessionOrThrow() throws RuntimeException when no session exists, meaning these methods were called without a prior transaction start or after the session ended.

Solutions

  1. Call beginTransaction() before commit()/rollBack()
  2. Check $connection->getSession() !== null before committing or rolling back
  3. Balance transaction calls so every commit/rollBack follows an active beginTransaction
  4. Use a transaction callback (e.g. transaction() helper) instead of manual begin/commit to guarantee pairing

Example fix

// before
$connection->rollBack(); // no transaction started
// after
$connection->beginTransaction();
try {
    // work
    $connection->commit();
} catch (Throwable $e) {
    $connection->rollBack();
    throw $e;
}
Defensive patterns

Strategy: type-guard

Validate before calling

if ($connection->getSession() === null) {
    $connection->beginTransaction();
}

Type guard

function hasActiveSession($connection): bool {
    return $connection->getSession() !== null;
}

Try / catch

try {
    $connection->commit();
} catch (RuntimeException $e) {
    if (str_contains($e->getMessage(), 'no active session')) {
        // transaction was never started or already closed — handle idempotently
    } else {
        throw $e;
    }
}

Prevention

When it happens

Trigger: Calling commit() or rollBack() on a MongoDB connection without calling beginTransaction() first, or after the transaction already committed/rolled back and the session was cleared.

Common situations: Code ported from SQL-based Laravel where implicit transactions exist; calling commit in a finally block after an exception already rolled the transaction back; unbalanced begin/commit calls across code paths.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.


AI-assisted analysis of mongodb/laravel-mongodb@0634653039 (2026-09-15). Data as JSON: /api/errors/0fb205e37509563d. Report an issue: GitHub.

Appendix: source

Thrown at src/Concerns/ManagesTransactions.php:48

    {
        return $this->session;
    }

    private function getSessionOrCreate(): Session
    {
        if ($this->session === null) {
            $this->session = $this->getClient()->startSession();
        }

        return $this->session;
    }

    private function getSessionOrThrow(): Session
    {
        $session = $this->getSession();

        if ($session === null) {
            throw new RuntimeException('There is no active session.');
        }

        return $session;
    }

    /**
     * Starts a transaction on the active session. An active session will be created if none exists.
     */
    public function beginTransaction(array $options = []): void
    {
        $this->runCallbacksBeforeTransaction();

        $this->getSessionOrCreate()->startTransaction($options);

        $this->handleInitialTransactionState();
    }

    private function handleInitialTransactionState(): void

View on GitHub (pinned to 0634653039)