MySQL & MariaDB error 1213: Deadlock found when trying to get lock; try restarting transaction
Deadlock found when trying to get lock; try restarting transaction (5.6.51) Two transactions each held a lock the other needed. InnoDB detected the cycle and rolled back one of them (yours) so the other could finish.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_LOCK_DEADLOCK | ER_LOCK_DEADLOCK |
| SQLSTATE | 40001 | 40001 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
A deadlock is a cycle: transaction A holds row 1 and wants row 2, transaction B holds row 2 and wants row 1. Neither can continue, so InnoDB picks a victim (usually the one with less work done), rolls back its whole transaction and returns 1213. The other transaction proceeds normally.
Occasional deadlocks are normal in busy systems. The application is expected to retry the transaction.
Why it happens
- Transactions touch the same rows in different orders (the classic money-transfer example in our reproduction).
- Gap and next-key locks under REPEATABLE READ: two sessions insert into the same index range after reading it with
SELECT … FOR UPDATE. - Foreign key checks and unique-index checks take locks on parent or index rows too.
- Large transactions that lock many rows increase the chance of overlap.
How to fix it
- Retry the whole transaction when you get 1213; it was rolled back completely, so it is safe to run again.
- Access rows in a consistent order in every code path (for example always by primary key ascending).
- Keep transactions small and short, and index the columns in
WHEREclauses of updates. - Read the details of the last deadlock:
SHOW ENGINE INNODB STATUS\G(section LATEST DETECTED DEADLOCK), or setinnodb_print_all_deadlocks = ONto log every one. - READ COMMITTED isolation uses fewer gap locks and often removes insert deadlocks.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE acct (id INT PRIMARY KEY, bal INT) ENGINE=InnoDB; INSERT INTO acct VALUES (1, 100), (2, 100);Session A: lock row 1.
USE demo; START TRANSACTION; UPDATE acct SET bal = bal - 10 WHERE id = 1;Session B: lock row 2.
USE demo; START TRANSACTION; UPDATE acct SET bal = bal - 5 WHERE id = 2;Session A: now ask for row 2 (it waits for B).
UPDATE acct SET bal = bal + 10 WHERE id = 2;Session B: ask for row 1. The cycle is complete: InnoDB rolls one transaction back with 1213 and the other continues.
UPDATE acct SET bal = bal + 5 WHERE id = 1;
You get: ERROR 1213 (40001) at line 1: Deadlock found when trying to get lock; try restarting transaction(11.8.9)
Verified fixes
Lock rows in the same order in every transaction (here: by id)
Make both sessions update the rows in the same order (row 1, then row 2). Session B now simply waits for A instead of deadlocking.
-- session A START TRANSACTION; UPDATE acct SET bal = bal - 10 WHERE id = 1; UPDATE acct SET bal = bal + 10 WHERE id = 2; COMMIT; -- session B START TRANSACTION; UPDATE acct SET bal = bal + 5 WHERE id = 1; UPDATE acct SET bal = bal - 5 WHERE id = 2; COMMIT;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Two transactions lock the same two rows in opposite order repro | ✓ 1213 | ✓ 1213 | ✓ 1213 | ✓ 1213 | ✓ 1213 | ✓ 1213 | ✓ 1213 | ✓ 1213 | ✓ 1213 |
| Lock rows in the same order in every transaction (here: by id) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |