DBMS Notes
Two-Phase Locking (2PL) is a concurrency control protocol that guarantees conflict serializability by organizing all lock operations of every transaction...
Introduction
Two-Phase Locking (2PL) is a concurrency control protocol that guarantees conflict serializability by organizing all lock operations of every transaction into exactly two distinct phases: a growing phase where locks are acquired and a shrinking phase where locks are released. Once a transaction releases any lock, it cannot acquire new locks — this restriction is what ensures serializable behavior.
The beauty of 2PL lies in its simplicity and power. By enforcing a single structural rule on when locks can be acquired and released, it guarantees that the concurrent execution of transactions is equivalent to some serial order — without needing to examine the actual schedule or build precedence graphs.
Why 2PL Guarantees Serializability
The lock point of each transaction defines its position in the equivalent serial schedule. If transaction T1's lock point comes before T2's lock point, then in the equivalent serial order, T1 comes before T2.
Example: 2PL in Action
| Transaction T1 | Transfer 100 from Account A to Account B |
| Transaction T2 | Calculate total balance of A + B |
| T1 Operations | T2 Operations: |
| T1's growing phase | acquires LOCK-X(A), then LOCK-X(B) |
| T1's lock point | after acquiring LOCK-X(B) |
| T1's shrinking phase | releases UNLOCK(A), then UNLOCK(B) |
| T2's growing phase | acquires LOCK-S(A), then LOCK-S(B) |
| T2's lock point | after acquiring LOCK-S(B) |
| T2's shrinking phase | releases UNLOCK(A), then UNLOCK(B) |
If T1 and T2 execute concurrently under 2PL, T2 must wait for T1 to release Lock-X(A) before it can acquire Lock-S(A). This waiting ensures T2 reads the updated value of A — maintaining consistency.
Variants of 2PL
Basic 2PL
The standard protocol described above. Guarantees conflict serializability but has two potential problems:
- Cascading rollbacks: If T1 releases a lock, T2 reads the item, and then T1 aborts — T2 must also abort because it read T1's uncommitted data.
- Deadlocks: Two transactions may each hold a lock the other needs.
Strict 2PL
Rigorous 2PL
Comparison of 2PL Variants
| Property | Basic 2PL | Strict 2PL | Rigorous 2PL |
|---|---|---|---|
| Conflict Serializable | Yes | Yes | Yes |
| Dirty Reads | Possible | No | No |
| Cascading Rollbacks | Possible | No | No |
| Concurrency Level | Highest | Medium | Lowest |
| Deadlock Possible | Yes | Yes | Yes |
| Implementation Complexity | Medium | Medium | Simple |
| Lock Release | After lock point | X-locks at commit | All at commit |
The Deadlock Problem with 2PL
Because transactions hold locks while waiting for other locks (during the growing phase), deadlocks can occur:
| T1: LOCK-X(A) | succeeds |
| T2: LOCK-X(B) | succeeds |
| T1: LOCK-X(B) | WAITS (held by T2) |
| T2: LOCK-X(A) | WAITS (held by T1) |
| T1 waits for T2, T2 waits for T1 | circular wait → DEADLOCK! |
Lock Conversion in 2PL
Sometimes a transaction first reads a data item (requiring shared lock) and later decides to write it (requiring exclusive lock). Lock upgrade allows this without violating 2PL:
| Lock Upgrade (S | X): Convert shared lock to exclusive lock |
| Lock Downgrade (X | S): Convert exclusive lock to shared lock |
| T1 | LOCK-S(A) ← growing phase |
| T1 | READ(A) |
| T1 | LOCK-UPGRADE(A to X) ← still growing phase (acquiring stronger lock) |
| T1 | WRITE(A) |
| T1 | LOCK-DOWNGRADE(A to S) ← shrinking phase begins |
| T1 | UNLOCK(A) ← shrinking continues |
Practical Implementation Considerations
Most modern database systems implement Strict 2PL as the default concurrency control mechanism:
- MySQL InnoDB: Uses Strict 2PL with automatic lock management (implicit locking on SQL statements)
- PostgreSQL: Uses a form of 2PL combined with MVCC (Multi-Version Concurrency Control) for read operations
- Oracle: Primarily uses MVCC but employs 2PL for write conflicts
Key Points to Remember
- 2PL divides lock operations into growing (acquire) and shrinking (release) phases
- Once a lock is released, no new lock can be acquired — this is the fundamental rule
- The lock point determines the transaction's position in the equivalent serial schedule
- Basic 2PL allows cascading rollbacks; Strict 2PL prevents them by holding X-locks until commit
- Rigorous 2PL holds all locks until commit — simplest but lowest concurrency
- 2PL can cause deadlocks (unlike timestamp-based protocols which are deadlock-free)
- Lock upgrades happen in the growing phase; lock downgrades happen in the shrinking phase
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Two-Phase Locking (2PL).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Database Management Systems (DBMS) topic.
Search Terms
dbms, database management systems (dbms), unit, two, phase, locking, two-phase locking (2pl)
Related Database Management Systems (DBMS) Topics