Page 3: Two-Phase Locking (2PL)

The Two-Phase Rule

Like a shopping trip - first you collect items (growing phase), then you check out and leave (shrinking phase).

Phase 1 - Growing Phase:

  • Can acquire new locks

  • Cannot release any locks

Phase 2 - Shrinking Phase:

  • Can release locks

  • Cannot acquire new locks

2PL Example:

Transaction T5:
Lock-S(A)     ← Growing phase
Lock-X(B)     ← Growing phase  
Unlock(A)     ← Shrinking phase (first unlock = phase change)
Unlock(B)     ← Shrinking phase
  1. 2PL does not prevent deadlocks:

  2. ensures conflict-serializability.

  3. it does not guarantee cascadeless schedules.

2PL Variants

1. Basic 2PL:

  • Ensures serializability but allows cascading rollbacks

  • Like dominoes falling - one failure causes others to fail

2. Strict 2PL:

  • Hold ALL exclusive locks until transaction commits

  • Prevents cascading rollbacks

  • Most commonly used in practice

3. Rigorous 2PL:

  • Hold ALL locks (shared + exclusive) until commit

  • Maximum safety, but reduced concurrency

Updated on