0
0
SQLquery~10 mins

Deadlock concept and prevention in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Deadlock concept and prevention
Transaction A locks Resource 1
Transaction B locks Resource 2
Transaction A requests Resource 2
Wait
Transaction B requests Resource 1
Wait
Deadlock detected: both wait forever
Deadlock prevention: Abort one transaction or use timeout
Two transactions each hold a resource and wait for the other, causing a deadlock; prevention involves detecting and resolving this cycle.
Execution Sample
SQL
-- Transaction A
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- Transaction B
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
Two transactions update different accounts and then try to access each other's locked resources, causing a deadlock.
Execution Table
StepTransactionActionResource LockedWaiting ForStatus
1ALock Resource 1 (account 1)Resource 1NoneLocks acquired
2BLock Resource 2 (account 2)Resource 2NoneLocks acquired
3ARequest Resource 2Resource 1Resource 2Waiting
4BRequest Resource 1Resource 2Resource 1Waiting
5SystemDetect deadlockResource 1 & 2Circular waitDeadlock detected
6SystemAbort Transaction BRelease Resource 2NoneDeadlock resolved
7AAcquire Resource 2Resource 1 & 2NoneLocks acquired, proceed
8ACommit TransactionRelease allNoneTransaction complete
9BRestart TransactionNoneNoneCan retry safely
💡 Deadlock detected at step 5 due to circular wait; resolved by aborting Transaction B at step 6
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 6After Step 7Final
Transaction A LocksNoneResource 1Resource 1Resource 1Resource 1Resource 1Resource 1 & 2None
Transaction B LocksNoneNoneResource 2Resource 2Resource 2NoneNoneNone
Transaction A StatusIdleRunningWaitingWaitingWaitingRunningRunningCommitted
Transaction B StatusIdleIdleRunningWaitingWaitingAbortedIdleRestarted
Key Moments - 3 Insights
Why does Transaction A wait at step 3 even though it already holds Resource 1?
Because Transaction A requests Resource 2 which is locked by Transaction B, so it must wait (see execution_table step 3).
How does the system detect the deadlock at step 5?
The system finds a circular wait: Transaction A waits for Resource 2 held by B, and B waits for Resource 1 held by A (execution_table step 5).
Why is Transaction B aborted to resolve the deadlock?
Aborting one transaction breaks the circular wait, releasing its locks so the other can proceed (execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what resource does Transaction B lock at step 2?
AResource 3
BResource 1
CResource 2
DNo resource locked
💡 Hint
Check the 'Resource Locked' column at step 2 in execution_table
At which step does the deadlock get resolved?
AStep 6
BStep 5
CStep 7
DStep 4
💡 Hint
Look for the step where a transaction is aborted and locks are released
If Transaction B was not aborted, what would happen at step 7?
ATransaction A would acquire Resource 2
BTransaction A would continue waiting
CTransaction B would release Resource 1
DDeadlock would be resolved automatically
💡 Hint
Refer to the waiting status of Transaction A and B in execution_table steps 3-7
Concept Snapshot
Deadlock occurs when two transactions wait for each other's locked resources.
It causes a cycle of waiting with no progress.
Prevention involves detecting cycles and aborting one transaction.
Use timeouts or lock ordering to avoid deadlocks.
Database systems detect and resolve deadlocks automatically.
Full Transcript
Deadlock happens when two transactions each hold a resource and wait for the other to release a resource. For example, Transaction A locks Resource 1 and waits for Resource 2, while Transaction B locks Resource 2 and waits for Resource 1. This creates a circular wait, causing both to wait forever. The system detects this deadlock and resolves it by aborting one transaction, releasing its locks so the other can continue. This process prevents the system from freezing. Understanding this flow helps avoid deadlocks by careful transaction design or using database features like timeouts and deadlock detection.