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
- Call beginTransaction() before commit()/rollBack()
- Check $connection->getSession() !== null before committing or rolling back
- Balance transaction calls so every commit/rollBack follows an active beginTransaction
- 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
- Prefer the transaction() callback helper over manual begin/commit
- Never call commit/rollBack in finally blocks without checking session state
- Keep begin/commit pairing inside a single method to avoid leaked transactions
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(): voidView on GitHub (pinned to 0634653039)