MySQL 8.4Lab-verified · 2026-11-29

MySQL ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

Two transactions were each waiting for a lock the other held, so InnoDB rolled one of them back.

MySQL 8.4.13
1213 (40001): Deadlock found when trying to get lock; try restarting transaction

Jump to a cause

  1. 01Two sessions update the same rows in opposite order
  2. 02Two sessions read with LOCK IN SHARE MODE, then both UPDATE

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
Setuprun as lab_a
CREATE TABLE accounts (account_id INT PRIMARY KEY, balance INT);
INSERT INTO accounts VALUES (1, 1000), (2, 1000);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproduce3 sessions open at the same time (A, B, C), run in this order
Session Arun as lab_a
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
OUTPUT
Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
Session Brun as lab_a
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE account_id = 2;
OUTPUT
Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
Session Arun as lab_a⏳ Waiting for the lock…
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
Session Brun as lab_a
UPDATE accounts SET balance = balance + 50 WHERE account_id = 1;
OUTPUT
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
Session A: the waiting statement finishesrun as lab_a
OUTPUT
Query OK, 1 row affected (2.30 sec)
Rows matched: 1  Changed: 1  Warnings: 0
Session C · Checkrun as root
SELECT name, count, status FROM information_schema.INNODB_METRICS
WHERE name = 'lock_deadlocks';
OUTPUT
+----------------+-------+---------+
| 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.

After the fix3 sessions open at the same time (A, B, C), run in this order
Session Arun as lab_a
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
OUTPUT
Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
Session B · Fixrun as lab_a⏳ Waiting for the lock…
START TRANSACTION;
UPDATE accounts SET balance = balance + 50 WHERE account_id = 1;
Session Arun as lab_a
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;
OUTPUT
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Query OK, 0 rows affected (0.00 sec)
Session B: the waiting statement finishesrun as lab_a
OUTPUT
Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (2.31 sec)
Rows matched: 1  Changed: 1  Warnings: 0
Session B · Fixrun as lab_a
UPDATE accounts SET balance = balance - 50 WHERE account_id = 2;
COMMIT;
OUTPUT
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Query OK, 0 rows affected (0.00 sec)
Session C · Checkrun as lab_a
SELECT account_id, balance FROM accounts;
OUTPUT
+------------+---------+
| account_id | balance |
+------------+---------+
|          1 |     950 |
|          2 |    1050 |
+------------+---------+
2 rows in set (0.00 sec)

02Two sessions read with LOCK IN SHARE MODE, then both UPDATE

A numbering table: sessions A and B both read the next number with LOCK IN SHARE MODE and both got 1. Shared locks do not block each other, but then each tried to UPDATE the row and needed an exclusive lock the other's shared lock prevented. B got ERROR 1213 and A's UPDATE went through.

If you read a row in order to change it, lock it for writing from the start. With SELECT ... FOR UPDATE session B waited at its SELECT until A committed, then read 2, and the final value was 3.

Setup
Setuprun as lab_a
CREATE TABLE seq_no (name VARCHAR(20) PRIMARY KEY, next_no INT);
INSERT INTO seq_no VALUES ('INVOICE', 1);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 1 row affected (0.00 sec)
Reproduce2 sessions open at the same time (A, B), run in this order
Session Arun as lab_a
START TRANSACTION;
SELECT next_no FROM seq_no WHERE name = 'INVOICE' LOCK IN SHARE MODE;
OUTPUT
Query OK, 0 rows affected (0.00 sec)

+---------+
| next_no |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)
Session Brun as lab_a
START TRANSACTION;
SELECT next_no FROM seq_no WHERE name = 'INVOICE' LOCK IN SHARE MODE;
OUTPUT
Query OK, 0 rows affected (0.00 sec)

+---------+
| next_no |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)
Session Arun as lab_a⏳ Waiting for the lock…
UPDATE seq_no SET next_no = next_no + 1 WHERE name = 'INVOICE';
Session Brun as lab_a
UPDATE seq_no SET next_no = next_no + 1 WHERE name = 'INVOICE';
OUTPUT
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
Session A: the waiting statement finishesrun as lab_a
OUTPUT
Query OK, 1 row affected (2.31 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Read-then-update patterns such as counters and stock checks should use FOR UPDATE, not a shared lock.

After the fix3 sessions open at the same time (A, B, C), run in this order
Session Arun as lab_a
START TRANSACTION;
SELECT next_no FROM seq_no WHERE name = 'INVOICE' FOR UPDATE;
OUTPUT
Query OK, 0 rows affected (0.00 sec)

+---------+
| next_no |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)
Session B · Fixrun as lab_a⏳ Waiting for the lock…
START TRANSACTION;
SELECT next_no FROM seq_no WHERE name = 'INVOICE' FOR UPDATE;
Session Arun as lab_a
UPDATE seq_no SET next_no = next_no + 1 WHERE name = 'INVOICE';
COMMIT;
OUTPUT
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Query OK, 0 rows affected (0.01 sec)
Session B: the waiting statement finishesrun as lab_a
OUTPUT
Query OK, 0 rows affected (0.00 sec)

+---------+
| next_no |
+---------+
|       2 |
+---------+
1 row in set (2.31 sec)
Session Brun as lab_a
UPDATE seq_no SET next_no = next_no + 1 WHERE name = 'INVOICE';
COMMIT;
OUTPUT
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Query OK, 0 rows affected (0.00 sec)
Session C · Checkrun as lab_a
SELECT name, next_no FROM seq_no;
OUTPUT
+---------+---------+
| name    | next_no |
+---------+---------+
| INVOICE |       3 |
+---------+---------+
1 row 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