251
MongoDBERRORCriticalTransactionsHIGH confidence

The specified transaction does not exist

What this means

This error occurs when a command like `commitTransaction` or `abortTransaction` is issued for a transaction that is not in progress or has already been completed. It often indicates a logic error in the application's transaction management.

Why it happens
  1. 1Calling `commitTransaction` twice on the same transaction
  2. 2The transaction timed out due to inactivity and was automatically aborted by the server
  3. 3A network error occurred, causing the driver to lose its state about the transaction while the server aborted it
  4. 4Trying to commit a transaction on a session that has not started one
How to reproduce

An application successfully commits a transaction and then, due to a logic bug, attempts to commit it again.

trigger — this will error
trigger — this will error
// Driver-level code (conceptual)
const session = client.startSession();
await session.withTransaction(async () => {
  // ... perform operations ...
});

// Later, due to a bug, the application tries to commit again
try {
  await session.commitTransaction();
} catch (e) {
  // This will throw NoSuchTransaction
  console.error(e);
}

expected output

MongoServerError: Cannot call commitTransaction twice on the same transaction.

Fix 1

Ensure Correct Transaction Logic

WHEN Managing transactions manually.

Why this works

Review your application code to ensure that `commitTransaction` or `abortTransaction` is called only once and only when a transaction is active. A transaction is completed after the first call to either commit or abort.

Fix 2

Use Driver's Transaction Helper API

WHEN Possible with your driver.

Use Driver's Transaction Helper API
// The 'withTransaction' helper manages the transaction state automatically.
await session.withTransaction(async (session) => {
  await collection.insertOne({ item: "book" }, { session });
  await collection.updateOne({ item: "pen" }, { $inc: { qty: -1 } }, { session });
}); // Commit/abort is handled automatically here.

Why this works

Most modern MongoDB drivers provide a high-level `withTransaction` (or similar) API that handles the start, commit, and abort logic for you, including retrying on transient errors. This greatly reduces the chance of state management bugs.

What not to do

Simply ignore the error

This error points to a significant flaw in your application's state management. Ignoring it can hide bugs that might lead to data inconsistency or incomplete operations.

Sources
Official documentation ↗

mongodb/mongo src/mongo/base/error_codes.yml

Driver Transaction APIs

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MongoDB errors