MySQL & MariaDB error 1205: Lock wait timeout exceeded; try restarting transaction
Lock wait timeout exceeded; try restarting transaction (5.6.51) A statement waited longer than innodb_lock_wait_timeout (50 seconds by default) for a row lock held by another transaction, and gave up. Only that statement is rolled back, not the whole transaction.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_LOCK_WAIT_TIMEOUT | ER_LOCK_WAIT_TIMEOUT |
| SQLSTATE | HY000 | HY000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
InnoDB locks the rows a transaction changes until it commits or rolls back. Another transaction that needs the same rows waits; after innodb_lock_wait_timeout seconds it gets 1205. Unlike a deadlock (1213), nothing is wrong with the waiting statement: it is blocked by someone else.
By default only the statement that timed out is rolled back; the rest of its transaction stays open (unless innodb_rollback_on_timeout is on), so your application must decide whether to retry the statement or roll back.
Why it happens
- A transaction that stays open too long: a script that updates rows and then does slow work before committing, or a forgotten
BEGINin an interactive session or GUI tool. - An
UPDATE/DELETEwithout a usable index locks far more rows than needed (it locks every row it scans). - Long batch jobs competing with online traffic for the same rows.
How to fix it
- Find the blocker while it is happening. On MySQL 8.0 use
SELECT * FROM sys.innodb_lock_waits\G; on MariaDB and older MySQL,information_schema.INNODB_TRXshows open transactions andtrx_mysql_thread_idtells you which connection to look at (orKILL). - Keep transactions short: do the slow work before
BEGIN, commit as soon as the writes are done. - Index the columns used in the
WHEREof updates and deletes, so InnoDB locks only the rows it changes. - Retry the statement in the application. Raising
innodb_lock_wait_timeoutonly makes the wait longer.
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);Session A: open a connection, start a transaction and change row 1, but don't commit.
USE demo; START TRANSACTION; UPDATE acct SET bal = 0 WHERE id = 1; -- leave this session openSession B: in a second connection, change the same row. It waits for A's lock (2 seconds here instead of the default 50).
USE demo; SET SESSION innodb_lock_wait_timeout = 2; UPDATE acct SET bal = 50 WHERE id = 1;Session A: COMMIT (or ROLLBACK) releases the lock.
COMMIT;
You get: ERROR 1205 (HY000) at line 1: Lock wait timeout exceeded; try restarting transaction(11.8.9)
Verified fixes
Find the blocking transaction while it waits (sys.innodb_lock_waits on MySQL; information_schema on MariaDB)
While session B is waiting, a third session shows who holds the lock. MySQL 8.0+:
SELECT waiting_pid, waiting_query, blocking_pid, blocking_query FROM sys.innodb_lock_waits;MariaDB and older MySQL: list open transactions and their connection ids, then KILL the blocker if needed.
SELECT trx_mysql_thread_id, trx_state, trx_started, trx_query FROM information_schema.INNODB_TRX; KILL 123; -- trx_mysql_thread_id of the blocking transaction
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Session A holds a row lock; session B waits longer than innodb_lock_wait_timeout repro | ✓ 1205 | ✓ 1205 | ✓ 1205 | ✓ 1205 | ✓ 1205 | ✓ 1205 | ✓ 1205 | ✓ 1205 | ✓ 1205 |
| Find the blocking transaction while it waits (sys.innodb_lock_waits on MySQL; information_schema on MariaDB) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |