Quick Summary: The error
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transactionoccurs in MySQL and MariaDB when an InnoDB transaction waits longer than the configuredinnodb_lock_wait_timeout(defaulting to 50 seconds) to acquire a row or gap lock held by another active session. Unlike deadlocks (ERROR 1213), which fail immediately, lock wait timeouts happen when a blocking transaction remains uncommitted, performs a slow full-table scan, or stalls during application-level work.
Understanding the Root Causes
MySQL’s InnoDB storage engine uses row-level locking to ensure ACID transaction isolation. When Session A modifies a row inside a transaction, it holds an exclusive lock until it issues COMMIT or ROLLBACK. If Session B tries to update or delete the same row, it blocks until Session A finishes or the timeout expires. Common triggers include:
- Uncommitted Hanging Transactions: An application opens a transaction (
BEGINorSET autocommit=0), updates rows, and then pauses for an external API call or crashes without committing, leaving locks held indefinitely. - Missing Indexes on UPDATE / DELETE Queries: Running an
UPDATEorDELETEwith aWHEREclause on an unindexed column forces InnoDB to perform a full table scan, locking every row in the table instead of just the target records. - Long-Running Batch Operations: Bulk data migrations,
ALTER TABLEmetadata locks, or unchunked batch updates holding locks longer than 50 seconds.
Step 1: Quick Fix – Identify and Clear the Blocking Transaction
Inspect InnoDB’s active lock wait graph and process list to locate the blocking session and terminate it safely if it is stuck:
-- 1. Inspect currently running processes and active transactions (MySQL 8.0+)
SELECT
trx_id,
trx_state,
trx_started,
trx_mysql_thread_id AS process_id,
trx_query
FROM information_schema.INNODB_TRX
ORDER BY trx_started ASC;
-- 2. In MySQL 8.0+, view exact blocking vs. waiting threads via performance_schema
SELECT
r.trx_id AS waiting_trx_id,
r.trx_mysql_thread_id AS waiting_thread,
r.trx_query AS waiting_query,
b.trx_id AS blocking_trx_id,
b.trx_mysql_thread_id AS blocking_thread,
b.trx_query AS blocking_query
FROM performance_schema.data_lock_waits w
JOIN information_schema.INNODB_TRX b ON b.trx_id = w.BLOCKING_ENGINE_TRANSACTION_ID
JOIN information_schema.INNODB_TRX r ON r.trx_id = w.REQUESTING_ENGINE_TRANSACTION_ID;
-- 3. Terminate the stuck blocking thread using its process_id
-- Replace 12345 with the blocking_thread ID from above
KILL 12345;
Step 2: Add Missing Indexes to Narrow Row Locks
If your UPDATE or DELETE statement is locking the entire table because the WHERE column is unindexed, inspect the execution plan and add an index:
-- 1. Check whether your query uses an index or performs a full scan
EXPLAIN UPDATE orders SET status = 'archived' WHERE customer_email = 'user@example.com';
-- If 'type' is ALL and 'key' is NULL, InnoDB is locking every row in the table!
-- 2. Add an index on the filter column to restrict locking strictly to matching rows
CREATE INDEX idx_orders_customer_email ON orders(customer_email);
Step 3: Tune innodb_lock_wait_timeout and Transaction Scope
If legitimate batch jobs require more than 50 seconds—or conversely, if you want web requests to fail fast rather than piling up—adjust innodb_lock_wait_timeout and keep transactions short:
-- 1. Check the current lock wait timeout (in seconds)
SHOW VARIABLES LIKE 'innodb_lock_wait_timeout';
-- 2. Adjust timeout for the current session or globally (e.g., 120s for migrations)
SET SESSION innodb_lock_wait_timeout = 120;
SET GLOBAL innodb_lock_wait_timeout = 120;
-- 3. Persist in /etc/mysql/mysql.conf.d/mysqld.cnf under [mysqld]
-- [mysqld]
-- innodb_lock_wait_timeout = 120
For application code, always move network calls, file I/O, and slow computations outside database transactions, and split bulk updates into small batches of 500–1,000 rows.
Verification & Testing Steps
Confirm that no stalled transactions remain in INNODB_TRX and re-run your query to verify clean execution:
-- 1. Verify no long-running blocking transactions remain active
SELECT COUNT(*) AS active_trx_count FROM information_schema.INNODB_TRX;
-- 2. Confirm the index is now used by your UPDATE statement
EXPLAIN UPDATE orders SET status = 'archived' WHERE customer_email = 'user@example.com';
-- 3. Re-run the transaction and commit cleanly
START TRANSACTION;
UPDATE orders SET status = 'archived' WHERE customer_email = 'user@example.com';
COMMIT;
Troubleshooting Summary Comparison
| Symptom / Scenario | Primary Root Cause | Recommended Solution |
|---|---|---|
| Stalled app or GUI client holding lock | Uncommitted transaction left open in INNODB_TRX |
Identify thread via data_lock_waits and run KILL <id> |
Simple UPDATE blocks unrelated rows |
Missing index causes full-table row lock scan | Verify with EXPLAIN and add an index on WHERE columns |
| Large migration or batch job times out | Single transaction exceeds 50s default timeout | Chunk updates into batches or raise innodb_lock_wait_timeout |
| Immediate failure vs. 50s wait | Circular lock (ERROR 1213) vs. linear wait (ERROR 1205) |
Ensure consistent lock ordering in code and retry deadlocks |
Leave a Reply