Lock wait timeout exceeded; try restarting transaction means your query waited too long for a row lock held by another transaction.
Why this happens
InnoDB uses row-level locking for transactional integrity. When transaction A holds a lock on rows that transaction B needs, B waits up to innodb_lock_wait_timeout seconds (default 50). If A doesnβt commit or rollback in time, B fails with this error. This is different from a deadlock β itβs a one-way wait.
What causes this error
- A long-running transaction is holding locks
- A query is updating many rows without an index (table lock)
- An uncommitted transaction from a crashed connection
Fix 1: Find the blocking transaction
-- Show which transactions are blocking
SELECT * FROM information_schema.innodb_lock_waits;
-- Show all running transactions
SHOW ENGINE INNODB STATUS;
-- Kill the blocking transaction
KILL <process_id>;
Fix 2: Increase the timeout (temporary)
SET innodb_lock_wait_timeout = 120; -- default is 50 seconds
Fix 3: Add missing indexes
If your UPDATE or DELETE doesnβt use an index, MySQL locks the entire table:
-- β No index on email β locks all rows
UPDATE users SET name = 'John' WHERE email = 'john@example.com';
-- β
Add an index
CREATE INDEX idx_users_email ON users(email);
Fix 4: Keep transactions short
Commit or rollback as soon as possible. Donβt hold transactions open while waiting for user input or external API calls.
Alternative solutions
Use SELECT ... FOR UPDATE NOWAIT (MySQL 8.0+) to fail immediately instead of waiting:
SELECT * FROM users WHERE id = 1 FOR UPDATE NOWAIT;
Break large updates into batches to reduce lock duration and release locks sooner.
Prevention
- Always ensure WHERE clauses in UPDATE/DELETE statements use indexed columns.
- Set
innodb_lock_wait_timeoutper-session for known long operations rather than globally.
Related: PostgreSQL: Connection Refused fix Β· MongoDB: Duplicate Key fix