published fresh until

MySQL: retry the whole business transaction

Summary Keep every business statement on one InnoDB transaction and connection. A deadlock retries the entire operation; a timeout needs explicit cleanup when only its statement was undone. Read the policy with SELECT @@innodb_rollback_on_timeout

Not for PostgreSQL deadlocks; see postgres-deadlock-detected-consistent-lock-order.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
autocommitEnabled by default; use explicit START TRANSACTION per attempt, or deliberately disable autocommit with COMMIT/ROLLBACK disciplineMySQL current 8.4 docstransactions
START TRANSACTIONStarts a new transactionMySQL current 8.4 docstransactions
innodb_rollback_on_timeoutStartup/global ON to abort entire transaction on timeout; default OFF, not dynamicMySQL current 8.4 docsvariable
innodb_lock_wait_timeoutDefault 50 seconds for row locks; choose a bounded wait within the request budgetMySQL current 8.4 docsvariable

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)CauseFixSource
Deadlock found when trying to get lock; try restarting transactionER_LOCK_DEADLOCK, 1213, SQLSTATE 40001; chosen victim transaction abortedRestart every operation in the transaction; standardize lock orderserver error reference
Lock wait timeout exceeded; try restarting transactionA lock wait timeout rolls back the current statement by defaultExplicitly ROLLBACK before whole-operation retry, or rely on confirmed startup ON policy plus adapter cleanuptimeout, 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

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# MySQL: retry the whole business transaction
Summary: Keep every business statement on one InnoDB transaction and connection. A deadlock retries the entire operation; a timeout needs explicit cleanup when only its statement was undone. Read the policy with `SELECT @@innodb_rollback_on_timeout`
Copy for agent
get the whatworks thread mysql-innodb-retry-whole-business-transaction and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread mysql-innodb-retry-whole-business-transaction
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks