Two-Phase Locking (2PL) is a specific lock-based protocol used in DBMS to ensure that concurrent transactions produce conflict serializable schedules. It is one of the most important and widely studied concurrency-control techniques.
The name “two-phase” comes from the fact that each transaction divides its life into two distinct phases with respect to locks: a growing phase and a shrinking phase.
What Is Two-Phase Locking (2PL)?
In 2PL, every transaction follows this rule:
Growing Phase
The transaction can:
Acquire new locks (shared or exclusive) on data items
But cannot release any lock
Shrinking Phase
The transaction can:
Release locks
But cannot acquire any new locks
Once a transaction starts releasing locks, it must never ask for a new lock again. This strict two-phase behavior is what gives 2PL its correctness guarantees.
Example Idea
Suppose a transaction T1 wants to read and write several data items (A, B, C). Under 2PL:
Growing Phase
T1 may:
Acquire
S-lockonAAcquire
X-lockonBAcquire
S-lockonC
Shrinking Phase
After some point, T1:
Releases lock on
AReleases lock on
BReleases lock on
C
But from this point on, T1 cannot request a new lock on, say, D.
This two-phase discipline prevents certain cyclic dependencies between transactions, which in turn ensures that the schedule is conflict serializable.
Why Two-Phase Locking Works
Prevents cycles in precedence graphs:
2PL avoids circular wait patterns that can lead to deadlocks or non-serializable schedules.Guarantees conflict serializability:
Any schedule produced by a 2PL-compliant system is conflict serializable, so transactions behave as if they ran one after another in some order.Simple but effective:
The rule is easy to state and implement in a DBMS, yet it gives strong correctness guarantees.
Variants of 2PL (Concept Only)
Several modified versions of 2PL exist, each with different trade-offs between serializability, recovery, and deadlock handling:
Strict 2PL:
Holds all exclusive locks until after commit to avoid recoverability problems.Strong Strict 2PL (Rigorous 2PL):
Holds all locks (shared and exclusive) until commit, giving even stronger guarantees.
These variants will be covered in more detail later, but for beginners, understanding the basic two-phase rule (growing vs shrinking) is the key.
Why Two-Phase Locking Matters?
It is the most common theoretical basis for lock-based concurrency control in real databases.
It explains how locking can ensure correctness without relying on serial execution.
It connects locking with serializability: once transactions follow 2PL, the DBMS can safely allow concurrent execution while still producing correct results.
Summary
Two-Phase Locking (2PL) in DBMS is a lock-based protocol where each transaction divides its life into two phases:
Growing phase: only acquire new locks.
Shrinking phase: only release locks, never acquire new ones.
This two-phase behavior ensures that the resulting schedule is conflict serializable, so concurrent transactions behave as if they ran one after another in some order. 2PL thus forms a core theoretical and practical tool for concurrency control in relational databases.