The Protocol
Two phase locking, or 2PL, is the classic way to guarantee serializability with locks. Each transaction runs in two phases that never overlap.
The Two Phases
- Growing phase: the transaction acquires locks as it needs them and releases none.
- Shrinking phase: once the transaction releases its first lock, it may never acquire another.
The single rule that no lock is taken after any lock is released is what produces serializable schedules. The point where growing turns into shrinking is the lock point.
Variants
- Strict 2PL holds all exclusive locks until commit or abort. This avoids cascading aborts because no other transaction reads uncommitted writes.
- Rigorous 2PL holds all locks, shared and exclusive, until commit. It is the most common form in real engines because it is simple to reason about.
The Cost
Basic 2PL guarantees serializability but does not prevent deadlocks. Two transactions can each hold a lock the other wants. Engines pair 2PL with deadlock detection or timeouts to recover.
Key idea
Two phase locking forbids acquiring any lock after releasing one, which guarantees serializability but still allows deadlocks.