← Lessons

quiz vs the machine

Gold1380

Databases

Two Phase Locking

Acquiring all locks before releasing any guarantees serializable schedules.

5 min read · core · beat Gold to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. What rule defines two phase locking?

2. What does strict 2PL add over basic 2PL?

3. Does 2PL by itself prevent deadlocks?