BIT202 ยท 2082 ยท 5 marks
What is two phase locking protocol? How can it lead to the deadlock condition?
What is two phase locking protocol? How can it lead to the deadlock condition? [5]
Quick answer
Two Phase Locking (2PL) is a concurrency control protocol that ensures serializability of transactions by regulating when locks can be acquired and released.
Two Phase Locking (2PL) Protocol
Definition
Two Phase Locking (2PL) is a concurrency control protocol that ensures serializability of transactions by regulating when locks can be acquired and released. It states that:
A transaction must acquire all the locks it needs before releasing any lock.
The protocol divides the execution of a transaction into two distinct phases:
The Two Phases
Phase 1: Growing Phase (Lock Acquisition Phase)
- A transaction may acquire locks (shared or exclusive).
- A transaction may NOT release any lock.
- The number of locks held by the transaction increases (or stays the same).
Phase 2: Shrinking Phase (Lock Release Phase)
- A transaction may release locks.
- A transaction may NOT acquire any new lock.
- The number of locks held decreases until all are released.
The point at which the last lock is acquired is called the Lock Point.
Number
of Locks
| /\
| / \
| / \
| / \
| / \
|___/ \___
|
+--Growing--+--Shrinking--> Time
Types of 2PL
| Type | Description |
|---|---|
| Basic 2PL | Locks released during shrinking phase |
| Strict 2PL | All exclusive locks held until commit/abort |
| Rigorous 2PL | All locks held until commit/abort |
How 2PL Can Lead to Deadlock
Deadlock occurs when two or more transactions are waiting for each other to release locks, forming a circular wait condition.
Example:
Consider two transactions T1 and T2:
| Step | T1 | T2 |
|---|---|---|
| 1 | Lock-X(A) -- success | |
| 2 | Lock-X(B) -- success | |
| 3 | Lock-X(B) -- WAIT (B locked by T2) | |
| 4 | Lock-X(A) -- WAIT (A locked by T1) |
Result:
- T1 holds lock on A and waits for B
- T2 holds lock on B and waits for A
- Neither can proceed -- DEADLOCK
Why 2PL Causes This:
In the growing phase, a transaction acquires locks one at a time (not all at once). This means:
- T1 acquires lock on A, then tries to acquire lock on B
- T2 acquires lock on B, then tries to acquire lock on A
- Since neither can release a lock during the growing phase (2PL rule), a circular dependency forms
This is a direct consequence of the 2PL rule that locks cannot be released until the shrinking phase begins, combined with the fact that locks are acquired incrementally.
Deadlock Handling
Since 2PL can cause deadlocks, the DBMS must handle them using:
- Deadlock Detection -- using a wait-for graph; if a cycle is detected, one transaction is rolled back (victim selection)
- Deadlock Prevention -- using timestamp-based schemes (Wait-Die or Wound-Wait protocols)
Summary
| Feature | Detail |
|---|---|
| Purpose | Ensures serializability |
| Phase 1 | Acquire locks only (Growing) |
| Phase 2 | Release locks only (Shrinking) |
| Deadlock | Possible due to incremental lock acquisition and circular wait |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.