0
0
MySQLquery~10 mins

Deadlock detection and prevention in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Deadlock detection and prevention
Transaction 1 starts
Transaction 1 locks Resource A
Transaction 2 starts
Transaction 2 locks Resource B
Transaction 1 requests Resource B
Transaction 2 requests Resource A
Deadlock detected
Deadlock resolution: rollback one transaction
Resources released, transactions continue
Two transactions lock different resources and then wait for each other's locked resource, causing a deadlock. The system detects this and rolls back one transaction to resolve it.
Execution Sample
MySQL
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- locks row with id=1

-- Meanwhile in another session:
START TRANSACTION;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- locks row with id=2

-- Back to first session:
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- waits for lock

-- Second session:
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- waits for lock, deadlock occurs
Two transactions update different rows and then try to update the other's locked row, causing a deadlock.
Execution Table
StepTransactionActionResource LockedWaits ForDeadlock DetectedResult
1T1START TRANSACTIONNoneNoneNoTransaction started
2T1UPDATE accounts SET balance = balance - 100 WHERE id=1Row id=1NoneNoRow id=1 locked by T1
3T2START TRANSACTIONNoneNoneNoTransaction started
4T2UPDATE accounts SET balance = balance + 100 WHERE id=2Row id=2NoneNoRow id=2 locked by T2
5T1UPDATE accounts SET balance = balance + 100 WHERE id=2Row id=1Row id=2NoT1 waits for lock on row id=2
6T2UPDATE accounts SET balance = balance - 100 WHERE id=1Row id=2Row id=1YesDeadlock detected: T1 waits for T2, T2 waits for T1
7SystemDeadlock resolutionNoneNoneYesRollback T2 to break deadlock
8T1Resumes and completesRow id=1 and id=2NoneNoT1 commits successfully
9T2Restart transactionNoneNoneNoT2 can retry after rollback
💡 Deadlock detected at step 6; system rolls back one transaction to resolve.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 6After Step 7Final
T1_locked_rowsNoneRow id=1Row id=1Row id=1Row id=1Row id=1None after commit
T2_locked_rowsNoneNoneRow id=2Row id=2Row id=2None after rollbackNone
T1_waiting_forNoneNoneNoneRow id=2Row id=2NoneNone
T2_waiting_forNoneNoneNoneNoneRow id=1NoneNone
Deadlock_statusNoNoNoNoYesResolvedNo
Key Moments - 3 Insights
Why does Transaction 1 wait at step 5?
At step 5 in the execution_table, Transaction 1 tries to lock row id=2, but Transaction 2 already holds that lock, so T1 must wait.
How does the system detect the deadlock?
At step 6, the system sees that T1 waits for T2 and T2 waits for T1, creating a cycle. This cycle is the deadlock detected.
What happens after the deadlock is detected?
At step 7, the system rolls back Transaction 2 to release its locks, allowing Transaction 1 to continue and complete.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what resource is Transaction 1 waiting for?
ANo resource, it proceeds
BRow id=1
CRow id=2
DRow id=3
💡 Hint
Check the 'Waits For' column at step 5 in the execution_table.
At which step does the deadlock get detected?
AStep 6
BStep 7
CStep 4
DStep 9
💡 Hint
Look for 'Deadlock Detected' column marked 'Yes' in the execution_table.
If the system did not roll back Transaction 2 at step 7, what would happen?
ABoth transactions would complete successfully
BDeadlock would persist and block both transactions
CTransaction 1 would automatically commit
DTransaction 2 would commit first
💡 Hint
Refer to the 'Result' column at step 7 and the concept of deadlock resolution.
Concept Snapshot
Deadlock occurs when two transactions wait for each other's locked resources.
MySQL detects deadlocks by finding cycles in waiting.
It resolves deadlocks by rolling back one transaction.
Use transactions carefully to minimize deadlocks.
Check locks and wait states to debug deadlocks.
Full Transcript
Deadlock happens when two transactions lock resources and each waits for the other to release a lock. In the example, Transaction 1 locks row id=1 and waits for row id=2 locked by Transaction 2. Transaction 2 locks row id=2 and waits for row id=1 locked by Transaction 1. This cycle causes a deadlock. MySQL detects this cycle and resolves it by rolling back one transaction, here Transaction 2. After rollback, Transaction 1 can continue and commit. Understanding which transaction waits for which resource helps detect deadlocks. Rolling back one transaction breaks the cycle and allows progress.