Few production incidents stall an entire PHP, Node.js, or .NET application faster than seeing logs fill up with SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction. Unlike a syntax bug you can reproduce locally in ten seconds, Error 1205 happens under live concurrency—when one database transaction holds an InnoDB row or gap lock longer than innodb_lock_wait_timeout (50 seconds by default) while a second worker queues up behind it.

Simply raising innodb_lock_wait_timeout = 300 only hides the symptom and exhausts PHP-FPM or connection-pool workers faster. Below is the exact diagnostic query for MySQL 8.x, the root causes behind InnoDB lock queues, and tested code fixes you can ship immediately.

What Causes MySQL Error 1205 (And How It Differs From Error 1213)?

MySQL InnoDB uses row-level locking during INSERT, UPDATE, DELETE, and SELECT ... FOR UPDATE statements. When Transaction A locks row #1042 and stalls (often waiting on an external HTTP call or uncommitted console session), Transaction B waits in queue. After innodb_lock_wait_timeout seconds, InnoDB aborts Transaction B with Error 1205—whereas Error 1213 (Deadlock) fails immediately when two transactions form a circular lock dependency.

MySQL Error Trigger Condition Fail Speed Primary Architectural Fix
Error 1205 (Lock Wait Timeout) Transaction B waits > 50s for Transaction A to release a row/table/gap lock Slow (blocks worker until timeout) Shorten transactions, add missing index, use SKIP LOCKED
Error 1213 (Deadlock) Circular wait (Tx A wants B’s lock, Tx B wants A’s lock) Instant (< 5 ms detection) Consistent lock ordering + bounded retry loop

Step 1: Find the Exact Blocking Query in MySQL 8.0 / 8.4 Right Now

In MySQL 8.x (which replaced legacy information_schema.INNODB_LOCK_WAITS), inspect performance_schema.data_lock_waits joined with INFORMATION_SCHEMA.PROCESSLIST to expose the exact blocking thread ID, how long it has been idle, and the SQL statement holding the lock:

SELECT
  r.trx_id                               AS waiting_trx_id,
  r.trx_mysql_thread_id                  AS waiting_thread,
  TIMESTAMPDIFF(SECOND, r.trx_wait_started, NOW()) AS wait_seconds,
  r.trx_query                            AS waiting_query,
  b.trx_id                               AS blocking_trx_id,
  b.trx_mysql_thread_id                  AS blocking_thread,
  TIMESTAMPDIFF(SECOND, b.trx_started, NOW()) AS blocking_trx_age_sec,
  p.INFO                                 AS blocking_current_sql,
  p.STATE                                AS blocking_process_state
FROM performance_schema.data_lock_waits w
INNER JOIN information_schema.innodb_trx b
  ON b.trx_id = w.blocking_engine_transaction_id
INNER JOIN information_schema.innodb_trx r
  ON r.trx_id = w.requesting_engine_transaction_id
INNER JOIN information_schema.processlist p
  ON p.ID = b.trx_mysql_thread_id
ORDER BY wait_seconds DESC;

If blocking_current_sql is NULL and blocking_process_state shows Sleep while blocking_trx_age_sec is climbing past 60 seconds, a backend script or developer GUI opened a transaction (`BEGIN`), ran an `UPDATE`, and never called `COMMIT` or `ROLLBACK`! As an emergency recovery step only, you can terminate that stuck blocker via KILL <blocking_thread>;.

Root Cause #1: Unindexed WHERE Conditions Lock the Entire Table

Developers often assume InnoDB “only locks matching rows.” In reality, InnoDB locks every index record it scans while searching for rows! If an UPDATE orders SET status = 'shipped' WHERE tracking_ref = 'TRK-991' statement runs without an index on `tracking_ref`, InnoDB performs a clustered table scan—and under `REPEATABLE READ`, locks every single row and gap in the entire table until `COMMIT`!

-- Before: Unindexed column scans and locks ALL 2M rows during UPDATE!
UPDATE jobs SET status = 'processing' WHERE queue_name = 'emails' AND status = 'pending' LIMIT 1;

-- Fix: Add a composite index matching the exact WHERE equality columns
CREATE INDEX idx_jobs_queue_status_id ON jobs (queue_name, status, id);

Root Cause #2: Queue Workers Contending on the Same Pending Rows (Use SKIP LOCKED)

If 10 cron workers or PHP queue consumers poll the same `jobs` or `outbox_events` table concurrently, Worker 1 locks row #501 while Workers 2–10 sit blocked waiting on row #501 until Error 1205 triggers. In MySQL 8.0+ and PostgreSQL 9.5+, **`FOR UPDATE SKIP LOCKED`** solves queue contention natively:

<?php

// CodeIgniter 4 / PHP 8.3: Lock the next available job without blocking other workers!
$db = \Config\Database::connect();
$db->transException(true)->transStart();

$job = $db->query(
    "SELECT id, payload_json
       FROM outbox_jobs
      WHERE status = 'pending'
      ORDER BY id ASC
      LIMIT 1
      FOR UPDATE SKIP LOCKED"
)->getRowArray();

if ($job !== null) {
    $db->table('outbox_jobs')
       ->where('id', $job['id'])
       ->update(['status' => 'claimed', 'claimed_at' => gmdate('Y-m-d H:i:s')]);
}

$db->transComplete();

// Execute external Stripe / Webhook / Email HTTP call OUTSIDE the DB transaction!
if ($job !== null) {
    dispatchExternalWebhook($job['payload_json']);
}

Root Cause #3: Calling External APIs Inside Open DB Transactions

Never execute `curl`, payment gateway requests, SMTP email sends, or LLM API calls inside `START TRANSACTION ... COMMIT`. If Stripe or an upstream API takes 8 seconds—or hangs for 60 seconds—every row locked by that transaction stays frozen for all other customers. Persist an intent row in a `< 5 ms` transaction, release all InnoDB locks, call the external API, and run a second short transaction to record the final result.

Root Cause #4: Switching Long Reporting Sessions to READ COMMITTED

Under MySQL’s default `REPEATABLE READ` isolation level, gap locks and next-key locks protect ranges against phantom reads—and `INSERT ... INTO archive SELECT ... FROM live_orders` locks source rows in `live_orders`! Using `SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;` for read-heavy reporting or archival batches eliminates gap locking across scanned ranges.

Prevention Checklist Before Deploying Database Code

Frequently Asked Questions

Should I increase innodb_lock_wait_timeout from 50 to 300 seconds?

Only as a temporary band-aid for a known long migration. On web-facing endpoints, raising the timeout keeps PHP-FPM workers and database connections tied up six times longer during an incident, turning a single slow transaction into a full site 502/504 outage. Fix the missing index or shorten the holding transaction instead.