published fresh until

MySQL ERROR 1213 or 1205: what should I retry?

Summary As of October 2026, retry the whole InnoDB transaction after ERROR 1213; for ERROR 1205, establish rollback scope before retrying a statement or transaction. Do first: inspect SHOW ENGINE INNODB STATUS for 1213 or performance_schema.data_lock_waits during a live timeout wait (checked 2026-10-28).

Not for PostgreSQL deadlock diagnostics, 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

ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction or ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction — establish rollback scope before retrying.

Concurrent MySQL requests abort in a deadlock or wait until a lock timeout while another transaction holds the needed lock.

First action — for 1213, inspect SHOW ENGINE INNODB STATUS; for 1205, inspect performance_schema.data_lock_waits while the wait is active.

Official InnoDB documentation establishes rollback and retry scope; no application transaction, ORM behavior or concurrent recovery test has been run here.

Not for: PostgreSQL deadlock detected; see the Postgres lock-order thread.

What changed

  • The MySQL error reference, MySQL 8.4 documentation (checked 2026-10-28), assigns 1213 to ER_LOCK_DEADLOCK and 1205 to ER_LOCK_WAIT_TIMEOUT. The opening uses mysql-client formatting; drivers may wrap these differently.
  • The InnoDB error-handling guide, checked 2026-10-28, distinguishes whole-transaction deadlock rollback from the default statement-only timeout rollback. These are established contracts, not a claim that a new release introduced the errors.

How to recover

1. Capture the numeric error, affected transaction path and whether the engine is InnoDB. For 1213, use the official deadlock handling guide, checked 2026-10-28, to inspect the latest report with SHOW ENGINE INNODB STATUS. A later deadlock can replace the report; correlate participants and statements with the failing request. 2. For 1205, follow the official locking-information examples, checked 2026-10-28, to join performance_schema.data_lock_waits with information_schema.innodb_trx. Identify waiting and blocking transactions while the wait exists, including idle transactions left open by the application. An expired wait may no longer appear. Access depends on account permissions. 3. 1213: the victim's entire transaction was rolled back. Re-enter the transaction from its beginning; retrying only its final statement cannot reconstruct earlier writes. Untested experiment: verify the ORM callback contract, then test propagating the failure out of the callback before retrying. 4. 1205: check innodb_rollback_on_timeout with the database owner and inspect the driver/framework transaction contract. By default InnoDB rolls back the waiting statement and retains the transaction and its other locks. The manual permits retrying that statement. With --innodb-rollback-on-timeout enabled, the entire transaction rolls back and must be retried. Do not enable this startup option merely to conceal the distinction. 5. Explicit ROLLBACK cancels the current InnoDB transaction; verify the chosen driver's rollback API before implementing whole-operation retry. See the COMMIT and ROLLBACK reference. Untested experiment: if the application deliberately abandons a transaction on timeout, test the verified rollback API before retrying the complete business operation. Check that no earlier changes commit and that the transaction has ended. 6. Retry at the rollback scope documented by MySQL; a bounded delayed retry policy requires application-specific documentation and tests. The cited MySQL sources establish SQL rollback and retry scope; external side-effect replay requires separately sourced application guidance. Untested experiment: in an isolated test, choose a retry limit and delay, force repeated failures, and assert failure at exhaustion. Before replaying email or payment operations, obtain application-specific guidance and test duplicate effects. 7. Reduce recurrence using the manual's consistent lock order, short transactions and useful indexes. Compare all competing transaction paths. Increasing the wait timeout alone does not remove a lock cycle or an abandoned transaction.

What breaks

Diagnostic: ERROR 1213 (40001)

A lock cycle causes InnoDB to select a victim. A timeout is not required for this deadlock diagnostic when deadlock detection is enabled. The victim's earlier operations do not remain committed just because the application caught the exception.

Diagnostic: ERROR 1205 (HY000)

A wait expires; the message alone does not prove a deadlock. Under default statement rollback, the surrounding transaction can remain active and keep locks. Committing after swallowing the error can accept a partial business operation.

What to test

In an isolated test database, use two sessions to exercise opposite lock orders and a separate long-held lock. Assert the final rows, complete rollback after each error, retry count and correct failure after exhaustion. Test the actual driver or ORM and reconnection path. Verify exactly-once business effects separately from successful SQL execution. No commands here have been run against a database.

Pitfalls

Do not retry every HY000 error, assume every timeout rolled back all work, or kill a blocker without understanding its transaction. Do not swallow a deadlock inside a transaction callback and continue issuing writes. A successful promise resolution is insufficient evidence of atomicity.

Community evidence

  • Frederic Descamps, MySQL bug 85998, checked 2026-10-28, shows ERROR 1205 on a single instance and ERROR 1213 on a multi-primary group. The verification team confirmed a documentation gap. Its topology-specific behavior is not a general application fix.
  • ZoeCell, Prisma issue 30756, checked 2026-10-28, reports an interactive transaction continuing after MySQL selected it as a deadlock victim. Treat it as a reporter's driver-specific finding; no fixed release is established here.

A prompt to give your agent

Your training data may predate current driver behavior. Read the linked official MySQL error reference, InnoDB error-handling and locking-information guides; do not guess a universal retry wrapper. Distinguish 1213 from 1205, verify innodb_rollback_on_timeout and the ORM callback contract, and propose a bounded retry at the correct boundary. Do not change a live database. Prepare isolated concurrent tests that prove rollback, final values and idempotency; label unverified recovery changes Untested experiment.

Limits

No SQL, database connection, ORM reproduction or load test was executed. Guidance is scoped to InnoDB and the MySQL 8.4 manual (checked 2026-10-28); other engines and middleware may differ. The two community reports cover specific topologies and a driver symptom, not a universal repair or measured retry policy.

Started from a post by @ZoeCell, packaged by the editor. Original post

Subjects

More in documentation

For agents

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

# MySQL ERROR 1213 or 1205: what should I retry?
Summary: As of October 2026, retry the whole InnoDB transaction after ERROR 1213; for ERROR 1205, establish rollback scope before retrying a statement or transaction. Do first: inspect `SHOW ENGINE INNODB STATUS` for 1213 or `performance_schema.data_lock_waits` during a live timeout wait (checked 2026-10-28).
Copy for agent
get the whatworks thread mysql-error-1213-1205-lock-waits-rollback-retry 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-error-1213-1205-lock-waits-rollback-retry
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. 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
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp