{"record":{"id":"62d53291dcdb23a2","repo":"mongodb/node-mongodb-native","slug":"transactions-are-not-supported-in-snapshot-session","errorCode":null,"errorMessage":"Transactions are not supported in snapshot sessions","messagePattern":"Transactions are not supported in snapshot sessions","errorType":"exception","errorClass":"MongoCompatibilityError","httpStatus":null,"severity":"error","filePath":"src/sessions.ts","lineNumber":390,"sourceCode":"\n  /** @returns whether this session is currently in a transaction or not */\n  inTransaction(): boolean {\n    return this.transaction.isActive;\n  }\n\n  /**\n   * Starts a new transaction with the given options.\n   *\n   * @remarks\n   * **IMPORTANT**: Running operations in parallel is not supported during a transaction. The use of `Promise.all`,\n   * `Promise.allSettled`, `Promise.race`, etc to parallelize operations inside a transaction is\n   * undefined behaviour.\n   *\n   * @param options - Options for the transaction\n   */\n  startTransaction(options?: TransactionOptions): void {\n    if (this.snapshotEnabled) {\n      throw new MongoCompatibilityError('Transactions are not supported in snapshot sessions');\n    }\n\n    if (this.inTransaction()) {\n      throw new MongoTransactionError('Transaction already in progress');\n    }\n\n    if (this.isPinned && this.transaction.isCommitted) {\n      this.unpin();\n    }\n\n    this.commitAttempted = false;\n    // increment txnNumber\n    this.incrementTransactionNumber();\n    // create transaction state\n    this.transaction = new Transaction({\n      readConcern:\n        options?.readConcern ??\n        this.defaultTransactionOptions.readConcern ??","sourceCodeStart":372,"sourceCodeEnd":408,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/sessions.ts#L372-L408","documentation":"Snapshot sessions provide a consistent read view of the data at a single point in time and cannot host a transaction. `startTransaction` checks `this.snapshotEnabled` (set when `startSession({ snapshot: true })`) and throws MongoCompatibilityError.","triggerScenarios":"`const session = client.startSession({ snapshot: true }); session.startTransaction();` — also reachable inside `withTransaction` if the session was created with snapshot mode.","commonSituations":"Reusing a session object across both snapshot reads and write transactions; generic helper that always calls `startSession({ snapshot: true })` and later tries to wrap writes in a transaction.","solutions":["Use a separate non-snapshot session for transactions: `client.startSession()` (no `snapshot: true`).","Design helpers to accept the session from the caller so snapshot vs. transaction sessions are not mixed.","If you need both, create two sessions with appropriate options."],"exampleFix":"// before\nconst session = client.startSession({ snapshot: true });\nsession.startTransaction();\n\n// after\nconst readSession = client.startSession({ snapshot: true });\nconst txnSession = client.startSession();\ntxnSession.startTransaction();","handlingStrategy":"validation","validationCode":"function txnSession(client) {\n  // do not enable snapshot for transaction-capable sessions\n  return client.startSession();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep read-only snapshot sessions and write transaction sessions separate.","Document each session's intended use at creation time.","Avoid generic 'always snapshot' helper sessions for all flows."],"tags":["sessions","transactions","snapshot","compatibility"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}