ParatuTech
Database journalMySQL

MySQL · Transaction handling

MySQL lock waits and safe transaction retries

Separate a lock wait from a deadlock, understand rollback scope, and define bounded retries without replaying unsafe side effects.

A slow transaction may be waiting for another transaction to release a lock. A retry policy needs the exact error and rollback scope before it decides what to repeat. MySQL 8.4 InnoDB treats deadlocks and lock wait timeouts differently.

01 One-way lock wait
T1T2Holds row ANeeds row Awaits forOne dependencyT2 → T1; no cycle shownLock release may let T2 proceedA timeout is a separate outcome
02 Cyclic lock wait
T1T2Holds A; needs BHolds B; needs ACYCLEInnoDB rolls back a victimWith deadlock detection enabledRetry the whole transactionBound attempts; back off; safe to repeat
Illustrative InnoDB row-lock dependencies. Deadlock detection must be enabled for immediate detection and victim selection. A lock wait timeout normally rolls back only the waiting statement; restore a clean transaction boundary before retrying. Bound retries and make external effects repeat-safe. Reconcile an uncertain COMMIT outcome instead of retrying blindly.

The short version

What to take away

  • A lock wait can clear; a deadlock is a cycle that needs an intervention.
  • InnoDB rolls back a deadlock victim transaction, but a lock timeout normally rolls back only the waiting statement.
  • Retry only eligible work from a clean transaction boundary, with a deadline and safe side effects.

Separate a queue from a cycle

In a simple wait, transaction B needs a row locked by A. A can finish and release it. In a deadlock, A also needs a resource held by B, forming a cycle.

With deadlock detection enabled, InnoDB selects a victim and rolls back its transaction. Disabling detection changes this behavior, so verify the configuration rather than assuming it. See deadlocks in InnoDB.

For an active data-lock wait, Performance Schema's data_lock_waits table connects blocked requests to blocking locks. Capture transaction and application context before considering any intervention.

Establish the rollback boundary

An InnoDB deadlock rolls back the whole victim transaction. A lock wait timeout normally rolls back only the statement that timed out; earlier statements in that transaction can remain active. The innodb_rollback_on_timeout setting changes timeout behavior. These distinctions are documented under InnoDB error handling.

For an application policy that retries the entire logical operation after either error, explicitly roll back any remaining transaction first and confirm cleanup. Start a fresh transaction and repeat the required reads and writes. Do not accidentally commit earlier partial work after a timeout.

Bound the retry and reduce contention

The following is a recommended application design, not a universal MySQL default:

  • Retry only recognized transient failures for operations designed to be retried. Use a small attempt limit, bounded backoff with jitter, and an overall request deadline.
  • Keep external side effects out of an unprotected retry loop. Repeating a transaction must not duplicate a payment, email or other action. A lost connection during COMMIT needs outcome reconciliation before replay.
  • Stop when the budget is exhausted and return a controlled failure with diagnostic context, excluding sensitive query values.

Keep transactions short and access shared rows in a consistent order. Review the latest deadlock report and query access paths to address recurring contention; see MySQL's deadlock handling guidance. Test failure paths and application invariants before approving a production change.

Discuss MySQL transaction handling with ParatuTech.

A considered next step

Bring the workload, the symptoms and the evidence. We’ll help you frame the next decision.

Discuss your database