# MongoServerError: Transaction number 1 on session 0x1234 is not the expected transaction number (expected 2)

- **ID:** `mongodb/transaction-number-mismatch`
- **Domain:** mongodb
- **Category:** protocol_error
- **Verification:** ai_generated
- **Fix Rate:** 88%

## Root Cause

A client attempted to commit or abort a transaction with a stale transaction number, likely due to a retry logic bug where the same session is reused without incrementing the transaction number, or a session is accidentally shared across concurrent transactions.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| MongoDB 4.2 | active | — | — |
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |

## Workarounds

1. **Ensure each transaction uses a new session or explicitly increments the transaction number. In Node.js driver: use `session.startTransaction()` and `session.commitTransaction()` within a loop that creates a new session for each attempt. Example: `const session = client.startSession(); session.startTransaction(); try { ... await session.commitTransaction(); } catch { await session.abortTransaction(); } finally { session.endSession(); }`** (90% success)
   ```
   Ensure each transaction uses a new session or explicitly increments the transaction number. In Node.js driver: use `session.startTransaction()` and `session.commitTransaction()` within a loop that creates a new session for each attempt. Example: `const session = client.startSession(); session.startTransaction(); try { ... await session.commitTransaction(); } catch { await session.abortTransaction(); } finally { session.endSession(); }`
   ```
2. **For retry logic, use a fresh session for each retry attempt instead of reusing the same session. In Python: `with client.start_session() as session: session.start_transaction(); ...` inside the retry loop.** (88% success)
   ```
   For retry logic, use a fresh session for each retry attempt instead of reusing the same session. In Python: `with client.start_session() as session: session.start_transaction(); ...` inside the retry loop.
   ```
3. **Check for accidental session sharing in async code (e.g., passing the same session object to multiple concurrent coroutines). Use `session.advanceTransactionNumber()` if driver supports it, or enforce sequential transaction processing per session.** (85% success)
   ```
   Check for accidental session sharing in async code (e.g., passing the same session object to multiple concurrent coroutines). Use `session.advanceTransactionNumber()` if driver supports it, or enforce sequential transaction processing per session.
   ```

## Dead Ends

- **** — The underlying session management bug in the code remains; the error will recur on the next transaction. (90% fail)
- **** — Killing the session only frees server resources; the client code still holds a stale session reference and will create a new session with the same problem. (85% fail)
- **** — The error is about transaction number sequencing, not timeout; changing timeouts has no effect. (95% fail)
