Two-phase locking and its variants
Shared and exclusive locks and their compatibility, the lock manager with waiting, the two-phase rule (growing phase then shrinking phase) and the proof that two-phase schedules are conflict serializable in the order of the lock points, why basic 2PL still allows dirty reads and cascading aborts, strict, rigorous and conservative 2PL, lock upgrades, deadlock in locking with the wait-for graph and the ways to handle it; locking is shown with a real lock manager and random lock schedules were checked against the theorem.
Why it exists
Serializability tells us which schedules are acceptable but not how to produce them. The most common method is locking: a transaction must hold a lock on an item before accessing it, and the lock manager makes other transactions wait when their requests are incompatible. Locking alone is not enough (a transaction could lock, use and release an item, then lock another); the two-phase rule makes it enough.
Prerequisites
You need serializability (schedules and serializability) and the idea of a deadlock with a cycle of waiting (deadlock conditions).
Precise definitions
Lock modes. , a shared (read) lock, and , an exclusive (write) lock. A transaction needs an S or X lock to read and an X lock to write. Requests are granted by the lock manager according to the compatibility matrix:
| held by another transaction ↓ / requested → | S | X |
|---|---|---|
| S | granted | wait |
| X | wait | wait |
Many readers can share an item; a writer is alone. A transaction that holds S may ask to upgrade to X; this succeeds only if nobody else holds a lock on the item.
The two-phase rule. A transaction is two-phase if all its lock requests come before its first unlock: a growing phase (acquire locks, release none) followed by a shrinking phase (release locks, acquire none). The moment of the last lock acquisition is the lock point.
Variants (they differ in when locks are released):
| variant | rule |
|---|---|
| basic 2PL | two-phase; locks may be released at any time in the shrinking phase |
| strict 2PL | two-phase, and exclusive locks are held until the transaction commits or aborts |
| rigorous 2PL | two-phase, and all locks (shared too) are held until commit or abort |
| conservative (static) 2PL | a transaction acquires all its locks before it starts executing; if it cannot get them all it waits holding none |
How it works
Why two-phase guarantees serializability
Theorem. Every schedule produced by transactions that obey the locking rules and are two-phase is conflict serializable, and the order of the lock points is an equivalent serial order.
Proof (lock points). Suppose the precedence graph has an edge : an operation of on came before a conflicting operation of on . The locks are incompatible, so released its lock on before acquired its lock on . By the two-phase rule 's lock point is before that release, and 's lock point is after that acquisition, so the lock point of is before the lock point of . A cycle would give a lock point before itself; hence the graph has no cycle, and sorting transactions by lock point is a topological order.
The converse is false: a schedule from non-two-phase transactions may happen to be serializable (the "not two-phase" row below). The textbooks quote a theorem that for every transaction that is not two-phase there is another (two-phase) transaction and a schedule of the two that is not serializable.
What basic 2PL does not give
Variants of two-phase locking, tested on one schedule each: start
Computed by a script, not typed| locking discipline (a sample schedule) | two-phase | strict 2PL | rigorous 2PL | conservative | conflict serializable | recoverable | avoids cascading aborts | strict |
|---|---|---|---|---|---|---|---|---|
| basic 2PL | ||||||||
| strict 2PL | ||||||||
| rigorous 2PL | ||||||||
| conservative 2PL | ||||||||
| not two-phase |
Step 0
Each schedule is a legal lock schedule (checked); the properties are computed from the definitions. A sample shows what a discipline allows, the guarantees of the discipline itself are the theorems on the page.
Each row is a legal lock schedule of one discipline. The basic 2PL sample is conflict serializable (; lock points at its second lock, later, in that order) but not recoverable: releases the exclusive lock on before committing, reads that value and commits first. If aborted afterwards, would have committed a dirty read. Holding exclusive locks to the end (strict) prevents reads and writes of uncommitted data, so schedules are strict and no cascading aborts occur; holding all locks to the end (rigorous) also prevents the write-after-uncommitted-read. In the rigorous variant the serial order is the order of commits.
The last row is not two-phase ( unlocks and later locks ) and yet its schedule is serializable: two-phase is sufficient, not necessary, for one schedule.
Systems normally use strict or rigorous 2PL.
A lock manager at work
Strict two-phase locking: a transaction waits until the other commits: start
Computed by a script, not typed| step | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| T1 | |||||||||
| T2 | |||||||||
| locks held | |||||||||
| waits for |
Step 0
T1 touches A and then B, T2 only A. All locks are held until commit (strict 2PL).
locks and updates and then ; when asks for the lock on it waits, because holds an exclusive lock. After commits all its locks are released and proceeds. The result is the serial schedule .
When the two-phase rule is dropped
Locking without the two-phase rule is not enough: start
Computed by a script, not typed| time | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| T1 | ||||||||||||||
| T2 |
Step 0
T1 reads A, then later writes B; T2 writes both in between. T1 releases its lock on A early.
Every request is legal and each item is protected when it is touched, but releases and acquires later (not two-phase). The schedule has the conflicts (on ) and (on ): not serializable.
Deadlock
Locking can make transactions wait for each other forever. The wait-for graph has an edge when waits for a lock held by ; a cycle is a deadlock (the circular wait of deadlock conditions).
Deadlock: each transaction waits for a lock held by the other: start
Computed by a script, not typed| step | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| T1 | ||||
| T2 | ||||
| locks held | ||||
| waits for |
Step 0
T1 writes A then B, T2 writes B then A.
holds and wants ; holds and wants . Two-phase locking does not prevent deadlock. Another common source is the upgrade:
Two read-modify-write transactions with shared then exclusive locks: start
Computed by a script, not typed| step | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| T1 | ||||
| T2 | ||||
| locks held | ||||
| waits for |
Step 0
Each transaction reads A (shared lock) and then writes A (it asks for an exclusive lock, an upgrade). Lock requests that cannot be granted make the transaction wait.
Both transactions hold shared locks on and both ask for the upgrade: each waits for the other to release. Taking the exclusive lock at the first access (when a read is followed by a write) avoids this particular deadlock.
Handling deadlock (as presented in the textbooks): prevention (conservative 2PL acquires everything at once, or impose an order on the items so that no cycle can form; the timestamp schemes wait-die and wound-wait decide who waits, quoted), detection (build the wait-for graph and look for a cycle, then choose a victim and roll it back, releasing its locks), or timeouts (abort a transaction that has waited too long). Conservative 2PL is deadlock free if all its locks are acquired atomically, but it needs to know the items in advance and reduces concurrency.
Worked examples
EX 1Is the schedule legal and two-phase
Easy: 's request is incompatible with 's exclusive lock, so must wait; the sequence is not a legal lock schedule until releases . : locks after releasing : not two-phase.
EX 2Lock point order
MediumFor the basic 2PL sample : the lock point of is its lock of (third operation), that of is its lock of (fifth). Order . The precedence graph has the single edge ( before ): consistent.
EX 3Deadlock
Medium: and : , interleaved as : after the first two operations each holds one lock; waits for and for : wait-for cycle . If both took the locks in the same order ( then ) no cycle could arise.
EX 4Typical questions
GATE level(a) "Which protocol guarantees conflict serializability?" Two-phase locking (and also strict, rigorous, conservative). (b) "Does 2PL guarantee freedom from deadlock?" No. (c) "Does strict 2PL guarantee freedom from deadlock?" No; it guarantees strict (hence cascadeless, recoverable) schedules. (d) "Which 2PL variant is deadlock free?" Conservative, since a transaction never waits while holding a lock. (e) "Serial order equivalent to a 2PL schedule?" The order of the lock points. (f) "Can a schedule of two-phase transactions be non-recoverable?" Yes, with basic 2PL (early release of exclusive locks).
Cheat box
Cheat box
- S and X locks; only S–S is compatible. Upgrade S to X if nobody else holds the item.
- Two-phase: growing then shrinking; lock point = last acquisition. 2PL conflict serializable, serial order = lock point order.
- Basic 2PL: not recoverable. Strict: X locks to the end. Rigorous: all locks to the end. Conservative: all locks first.
- 2PL, strict and rigorous 2PL can deadlock; conservative cannot. Detect with the wait-for graph.
- 2PL is sufficient, not necessary, for serializability.
Traps and GATE patterns
Connections
Builds on
Across subjects
The wait-for graph is the resource allocation graph of the operating system with transactions as processes (deadlock conditions). Timestamp ordering (timestamp ordering) is a different way to guarantee serializability without locks. The classes of schedules strict 2PL produces are those of recoverability.
PYQs
6 previous year questions from your book. I re-solved each one myself (2 by running or simulating, 4 by reasoning only); the book's key is shown next to my answer. All PYQs
A company maintains records of sales made by its salespersons and pays them commission based on each individual's total sales made in a year. This data is maintained in a table with following schema: salesinfo = (salespersonid, totalsales, commission) In a certain year, due to better business results, the company decides to further reward its salespersons by enhancing the commission paid to them as per the following formula: If commission < = 50000, enhance it by 2% If 50000 < commission < = 100000, enhance it by 4% If commission > 100000, enhance it by 6% The IT staff has written three different SQL scripts to calculate enhancement for each slab, each of these scripts is to run as a separate transaction as follows: DATABASE MANAGEMENT SYSTEM T1 Update salesinfo Set commission = commission * 1.02 Where commission < = 50000; T2 Update salesinfo Set commission = commission * 1.04 Where commission > 50000 and commission is < = 100000; T3 Update salesinfo Set commission = commission * 1.06 Where commission > 100000; Which of the following options of running these transactions will update the commission of all salespersons correctly
- (a)Execute T1 followed by T2 followed by T3
- (b)Execute T2, followed by T3; T1 running concurrently throughout
- (c)Execute T3 followed by T2; T1 running concurrently throughout
- (d)Execute T3 followed by T2 followed by T1
Which of the following statements about the Two Phase Locking (2PL) protocol is/are TRUE?
- (a)2PL permits only serializable schedules
- (b)With 2PL, a transaction always locks the data item being read or written just before every operation and always releases the lock just after the operation
- (c)With 2PL, once a lock is released on any data item inside a transaction, no more locks on any data item can be obtained inside that transaction
- (d)A deadlock is possible with 2PL
Consider the following two statements about database transaction schedules: I. Strict two-phase locking protocol generates conflict serializable schedules that are also recoverable. II. Timestamp-ordering concurrency control protocol with Thomas write rule can generate view serializable schedules that are not conflict serializable. Which of the above statements is/are TRUE?
- (a)Both I and II
- (b)Neither I nor II
- (c)II only
- (d)I only DATABASE MANAGEMENT SYSTEM
Consider the following two phase locking protocol.
Suppose a transaction T accesses (for read or write
operations), a certain set of objects {O1,.....Ok}. This is
done in the following manner:
Step 1. T acquires exclusive locks to O1,....Ok in
increasing order of their addresses.
Step 2. The required operations are performed.
Step 3. All locks are released.
This protocol will ?- (a)guarantee serializability and deadlock-freedom
- (b)guarantee neither serializability nor deadlock- freedom
- (c)guarantee serializability but not deadlock- freedom
- (d)guarantee deadlock-freedom but not serializability
Which of the following concurrency control protocols ensure both conflict serializability and freedom from deadlock? I. 2-phase locking II. Time-stamp ordering
- (a)I only
- (b)II only
- (c)Both I and II
- (d)Neither I nor II
Consider the following two transactions: T1 and T2. T1: read(A); read(B); if A = 0 then B ← B + 1; write(B); T2: read(B); read(A); if B ≠ 0 then A ← A - 1; write(A); Which of the following schedules, using shared and exclusive locks, satisfy the requirements for strict two- phase locking (Strict 2PL) for the above transactions?
- (a)S1 : lock S(A); read (A); lock S(B); read(B); ifA = 0 then B ←B + 1; write(B); commit; unlock(A); unlock (B); S2 : lock S(B); read
- (b); lock S(A); read(A); ifB≠0 then A ←A -1; write(A); commit; unlock(B); unlock (A);
- (b)S1: lock X(A); read(A); lock X(B); read(B); if A = 0 then B ← B + 1; write(B); unlock(A); commit; unlock(B); S2: lock X(B); read(B); lock X(A); read(A); if B ≠ 0 then A ← A - 1; write(A); unlock(A); commit; unlock(A);
- (c)S1: lock S(A); read(A); lock X(B); read(B); if A = 0 then B ← B + 1; write(B); unlock(A); commit; unlock (B); S2: lock S(B); read(B); lock X(A); read(A); if B ≠ 0 then A ← A - 1; write(A); unlock(A); commit; unlock (A);
- (d)S1: lock S(A); read(A); lock X(B); read(B); if A = 0 then B ← B + 1; write(B); unlock(A); unlock(B); commit; S2: lock S(B); read(B); lock X(A); read(A); if B ≠ 0 then A ← A - 1; write(A); unlock(A); unlock(B); commit;
Self-check
Question 1
: . Is two-phase?
Show answerHide
Yes: both locks are acquired before the first unlock. Its lock point is the lock of .
Question 2
Which two-phase variant guarantees that no transaction ever waits while holding a lock?
Show answerHide
Conservative 2PL (all locks are acquired before the transaction starts).
Question 3
Why can a schedule of basic 2PL transactions be non-recoverable?
Show answerHide
A transaction may release an exclusive lock before committing; another transaction can then read the value and commit first.
Revision card
One-screen revision
- Lock S for read, X for write; S–S only compatible. Growing phase, then shrinking phase.
- 2PL conflict serializable (lock point order). Not necessary.
- Strict: X locks to the end (strict, cascadeless, recoverable). Rigorous: all locks. Conservative: all first, no deadlock.
- Deadlock = cycle in the wait-for graph; 2PL does not prevent it.
References
- ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Chapter 18, Sections 18.1 and 18.2 (Lock-based protocols, deadlock handling)
- ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Chapter 21, Section 21.1 (Two-phase locking techniques)
Verification
scripts/enum-checks/db_txn_check.py: (1) 20000 random lock schedules were generated (lock before first access, shared then upgrade to exclusive, unlocks either all after the last access or right after the last access of each item); of the legal ones, every two-phase schedule was conflict serializable and the order of the lock points was among the conflict equivalent serial orders; legal schedules that are not two-phase and not serializable exist; (2) 3000 random programs were run on the lock manager of scripts/db_txn.py (strict 2PL: locks taken just before the access, upgrade when needed, all released at commit) in random order: every completed schedule was conflict serializable, strict and rigorous, and whenever every unfinished transaction was blocked the wait-for graph contained a cycle (deadlock found in random runs). The proof of the theorem on the page is the usual lock-point argument, written for this site. QUOTED, not verified here: the theorem that if one transaction is not two-phase a non-serializable schedule of the set exists (Eswaran et al.); deadlock freedom of conservative 2PL (follows if all locks are acquired atomically, assumed); wait-die and wound-wait described from the textbooks; the behaviour of real systems (multi-granularity locks, update locks, snapshot isolation) is outside this page. Textbook section numbers are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and re-derived against the textbook definitions and the sample database. DEFECT FOUND AND FIXED: a stray punctuation mark was removed and the theorem about transactions that are not two-phase was restated in its textbook form. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)