Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4
A deadlock happens when transaction A waits for a row locked by B while B waits for a row locked by A. Neither can ever finish, so InnoDB picks one and fails it with ERROR 1213.
In both experiments the error came immediately, without waiting for a timeout, in session B, the session that closed the loop. Right after that, session A's waiting UPDATE finished: B's locks had been released. So the application that gets 1213 must run the whole transaction again, not just the last statement.
01Two sessions update the same rows in opposite order
A money transfer: session A updated account 1 then account 2, session B updated account 2 then account 1. A's second UPDATE waited for B; when B's second UPDATE asked for account 1, B got ERROR 1213 at once and A's UPDATE completed (2.30 sec including the wait).
information_schema.INNODB_METRICS keeps a running count in lock_deadlocks (here 9, counted since the server started). The fix is to lock rows in the same order everywhere. In the retry both sessions started with account 1: B simply waited for A's COMMIT, then continued. Both transfers committed and the balances became 950 and 1050.
Setup
CREATE TABLE accounts (account_id INT PRIMARY KEY, balance INT);
INSERT INTO accounts VALUES (1, 1000), (2, 1000);Query OK, 0 rows affected (0.02 sec)
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE account_id = 2;Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;UPDATE accounts SET balance = balance + 50 WHERE account_id = 1;ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transactionQuery OK, 1 row affected (2.30 sec)
Rows matched: 1 Changed: 1 Warnings: 0SELECT name, count, status FROM information_schema.INNODB_METRICS
WHERE name = 'lock_deadlocks';+----------------+-------+---------+
| name | count | status |
+----------------+-------+---------+
| lock_deadlocks | 9 | enabled |
+----------------+-------+---------+
1 row in set (0.00 sec)Sorting keys before updating them (for example by primary key) removes most deadlocks in batch jobs and transfers.
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0START TRANSACTION;
UPDATE accounts SET balance = balance + 50 WHERE account_id = 1;UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (2.31 sec)
Rows matched: 1 Changed: 1 Warnings: 0UPDATE accounts SET balance = balance - 50 WHERE account_id = 2;
COMMIT;Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Query OK, 0 rows affected (0.00 sec)SELECT account_id, balance FROM accounts;+------------+---------+
| account_id | balance |
+------------+---------+
| 1 | 950 |
| 2 | 1050 |
+------------+---------+
2 rows in set (0.00 sec)FAQ
Which transaction gets rolled back on ERROR 1213?
In both experiments here it was session B, the one whose request completed the cycle, and the error came immediately. Session A's waiting statement finished right after because B's locks were released.
How do I fix Deadlock found when trying to get lock?
Make every transaction lock rows in the same order, and use SELECT ... FOR UPDATE when you read a row you are about to change. Both fixes are shown above with the rerun output. Also retry the whole transaction in code when 1213 does happen.
How can I see how many deadlocks happened?
SELECT name, count FROM information_schema.INNODB_METRICS WHERE name = 'lock_deadlocks'; shows a cumulative counter.
Is 1213 the same as lock wait timeout 1205?
No. 1205 means a statement waited too long for a lock. 1213 means two transactions wait for each other; MySQL detects it and rolls one back without waiting.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13