Two-Phase Locking (2PL) Protocol: Short Notes for Concurrency Control
Two-Phase Locking (2PL) is a locking-based concurrency control protocol used to ensure that interleavings of transactions remain conflict-serializable. The core idea is that each transaction follows a strict two-stage discipline for acquiring and releasing locks: it first grows its set of locks, and only then shrinks by releasing locks. In particular:
During the growing phase, a transaction may acquire (shared/exclusive) locks, but it must not release any held lock. Once it enters the shrinking phase, it may release locks, but it must not acquire any new locks. This separation prevents “lock conversion/release-then-reacquire” patterns that can otherwise produce schedules that are not conflict-serializable.
Key terms:
- Two-Phase Locking
- Growing phase
- Shrinking phase
- Shared lock (S-lock)
- Exclusive lock (X-lock)
Limitation note: I’m unable to perform the required web searches right now due to a tool usage limitation (external sources/citations are unavailable). The content below is based on standard database concurrency-control theory.
Two-Phase Locking (2PL) - Growing and Shrinking Phases (Conceptual)
Locking compatibility and why 2PL works
2PL relies on the compatibility rules of lock types:
- S-locks are compatible with S-locks (multiple readers allowed).
- X-locks are incompatible with S-locks and X-locks (writers require exclusive access).
Let’s model the state of a transaction as the set of locks it holds over time. The 2PL discipline enforces that:
- every lock acquisition for occurs before its first lock release,
- after the first release, cannot acquire new locks.
This ordering property is what supports the proof that 2PL schedules are conflict-serializable: intuitively, it prevents cycles in the precedence (conflict) graph that arise when transactions alternate between acquiring and releasing locks.
Mermaid view of the “no switching back” rule:
How a Transaction Follows 2PL
- 1Step 1
As executes, it requests S-locks for reads and X-locks for writes. It cannot release any lock yet.
- 2Step 2
Once releases any lock, it permanently transitions to shrinking.
- 3Step 3
After entering shrinking, may release locks but must not request any additional locks.
- 4Step 4
At the end, remaining locks are released by transaction completion (consistent with the shrinking rule).
Minimal and common variations: Strict 2PL (rigorous timing)
A widely used strengthening is Strict 2PL (also called rigorous 2PL). The key rule is:
- A transaction holds all X-locks until it commits or aborts (i.e., it cannot release write locks early).
This design prevents cascading aborts, because no other transaction can read uncommitted (dirty) data written by a transaction that later aborts.
Key terms:
- Strict 2PL
- Cascading abort
- Dirty read
- Recoverability
Callout intuition: strict 2PL is more conservative (can reduce concurrency) but is very safe for recovery.
Pro Tip
When writing short notes for exams, always mention: (1) growing vs shrinking phases, (2) “no lock acquisition after first unlock,” and (3) optional upgrade to strict 2PL to prevent cascading aborts.
Common Pitfall
Don’t confuse 2PL with mere “lock correctness.” The two-phase constraint (acquire then release only) is what guarantees conflict-serializability; releasing early without a shrinking phase can break the guarantee.
2PL Transaction Timeline
Growing
Phase 1Acquire locks (S for read, X for write). No releasing."
First lock release
BoundaryTransaction enters shrinking phase here."
Shrinking
Phase 2Release locks. No further lock acquisitions."
Commit/Abort
EndFinal completion; locks are released (consistent with shrinking)."
Quick Exam-Ready Notes
Effect of 2PL Variants on Concurrency vs Safety
Higher safety usually reduces concurrency due to stricter lock holding.
Knowledge Check
In Two-Phase Locking (2PL), what is the key restriction after a transaction releases its first lock?
Explore Related Topics
Object-Oriented DBMS (OODBMS): Short Notes & Core Concepts
Post-Correspondence Problem (PCP): A Structured “Short Note” for Learning
Inverted Page Table
An inverted page table (IPT) is a global paging structure that keeps one entry per physical frame, recording the virtual page, process ID, and status bits, thus reducing page‑table memory.
- Fields: frame index, VPN, PID/ASID, control bits, hash/link.
- Entries = Physical memory ÷ page size (e.g., ).
- Lookup uses PID + VPN key; hashing and TLB hide most cost.
- Benefit: memory usage depends on frames, not on all virtual pages.
- Cost: slower translation than direct indexing; relies on hashing/TLB.