Introduction
Deadlocks happen when two or more database transactions wait for each other to finish, causing a standstill. Detecting and preventing deadlocks helps keep the database working smoothly without freezing.
SHOW ENGINE INNODB STATUS;
SHOW ENGINE INNODB STATUS;
SET innodb_lock_wait_timeout = 50;
START TRANSACTION;
-- your SQL statements
COMMIT;SHOW ENGINE INNODB STATUS;
-- T1: START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;-- T2: START TRANSACTION;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;START TRANSACTION;
UPDATE products SET stock = stock - 1 WHERE id = 10;
UPDATE orders SET status = 'processed' WHERE id = 100;
COMMIT;Pass the test and this pattern moves unknown