What this thread does
Do first: inspect SELECT @@autocommit, @@innodb_rollback_on_timeout, @@innodb_lock_wait_timeout and put the retry boundary around the complete business transaction.
Short answer
As of November 2026, this card targets MySQL 8.4.13, October 20, 2026, with all participating tables using InnoDB. Explicitly clean up the failed attempt and replay all business reads and writes in a new transaction.
Choose / don't choose
- Whole-operation retry: choose after a known aborted transaction; don't replay only the last SQL statement after a deadlock (error handling).
- Startup rollback-on-timeout policy: choose when timeout should abort all transaction statements; don't assume it is a dynamic session switch (variable).
- External effects: choose a separate idempotent delivery design for email/payment/messages; don't expect SQL rollback to undo a remote call. This is an application boundary, not an InnoDB guarantee.
Not for: PostgreSQL deadlocks; see postgres-deadlock-detected-consistent-lock-order.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
autocommit | Enabled by default; use explicit START TRANSACTION per attempt, or deliberately disable autocommit with COMMIT/ROLLBACK discipline | MySQL current 8.4 docs | transactions |
START TRANSACTION | Starts a new transaction | MySQL current 8.4 docs | transactions |
innodb_rollback_on_timeout | Startup/global ON to abort entire transaction on timeout; default OFF, not dynamic | MySQL current 8.4 docs | variable |
innodb_lock_wait_timeout | Default 50 seconds for row locks; choose a bounded wait within the request budget | MySQL current 8.4 docs | variable |
Turning autocommit back to 1 can itself commit; restore pooled session state only after closing the attempt (implicit commits).
Order of operations
1. Reserve one connection; begin the transaction before reads used to decide writes. Keep retry count/deadline outside this transaction, and take locks in a consistent order. An attempt includes all statements that implement the business invariant. 2. On success COMMIT once. On a known deadlock, the server has already aborted the transaction; let the transaction API complete cleanup before starting a new attempt. On timeout with rollback-on-timeout OFF, explicitly ROLLBACK the still-open transaction before releasing the connection or retrying the operation. 3. Apply bounded backoff outside the transaction, then begin again and repeat all reads and writes. Exhaustion returns failure; it must not fall through into COMMIT. If cleanup fails, discard the connection. Diagnose repeated cycles using SHOW ENGINE INNODB STATUS; Peiran Song's October 28, 2014 report describes a cycle of lock dependencies (official handling). 4. Keep ALTER TABLE, other implicit-commit DDL and transaction-control nesting outside the retry closure. These statements implicitly end any transaction active in the current session, as if you had done a COMMIT before executing the statement (implicit commits). 5. In disposable fixtures, force two connections to lock rows in opposite order; require one complete result per successful operation after retry. Separately force a timeout after an earlier valid write, abandon the attempt, and require that earlier write absent. Assert the final invariant from another connection.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
Deadlock found when trying to get lock; try restarting transaction | ER_LOCK_DEADLOCK, 1213, SQLSTATE 40001; chosen victim transaction aborted | Restart every operation in the transaction; standardize lock order | server error reference |
Lock wait timeout exceeded; try restarting transaction | A lock wait timeout rolls back the current statement by default | Explicitly ROLLBACK before whole-operation retry, or rely on confirmed startup ON policy plus adapter cleanup | timeout, scope |
Missing
- Retry count/backoff and operation idempotency need application policy. An ambiguous network failure during COMMIT does not establish whether commit occurred; reconcile by a durable operation identity rather than blindly applying it twice.
Started from a post by @Oracle MySQL, packaged by the editor. Original post