G27
Self-checkedTier 1 8 min readdbms.two-phase-locking

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. S(X)\text{S}(X), a shared (read) lock, and X(X)\text{X}(X), 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 →SX
Sgrantedwait
Xwaitwait

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):

variantrule
basic 2PLtwo-phase; locks may be released at any time in the shrinking phase
strict 2PLtwo-phase, and exclusive locks are held until the transaction commits or aborts
rigorous 2PLtwo-phase, and all locks (shared too) are held until commit or abort
conservative (static) 2PLa 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 Ti→TjT_i \to T_j: an operation of TiT_i on XX came before a conflicting operation of TjT_j on XX. The locks are incompatible, so TiT_i released its lock on XX before TjT_j acquired its lock on XX. By the two-phase rule TiT_i's lock point is before that release, and TjT_j's lock point is after that acquisition, so the lock point of TiT_i is before the lock point of TjT_j. 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. ■\blacksquare

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-phasestrict 2PLrigorous 2PLconservativeconflict serializablerecoverableavoids cascading abortsstrict
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.

0 / 5
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

Each row is a legal lock schedule of one discipline. The basic 2PL sample is conflict serializable (T1→T2T_1 \to T_2; lock points T1T_1 at its second lock, T2T_2 later, in that order) but not recoverable: T1T_1 releases the exclusive lock on AA before committing, T2T_2 reads that value and commits first. If T1T_1 aborted afterwards, T2T_2 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 (T1T_1 unlocks AA and later locks BB) 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
step123456789
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).

0 / 9
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

T1T_1 locks and updates AA and then BB; when T2T_2 asks for the lock on AA it waits, because T1T_1 holds an exclusive lock. After T1T_1 commits all its locks are released and T2T_2 proceeds. The result is the serial schedule T1 T2T_1\,T_2.

When the two-phase rule is dropped

Locking without the two-phase rule is not enough: start

Computed by a script, not typed
time1234567891011121314
T1
T2

Step 0

T1 reads A, then later writes B; T2 writes both in between. T1 releases its lock on A early.

0 / 14
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

Every request is legal and each item is protected when it is touched, but T1T_1 releases AA and acquires BB later (not two-phase). The schedule has the conflicts T1→T2T_1 \to T_2 (on AA) and T2→T1T_2 \to T_1 (on BB): not serializable.

Deadlock

Locking can make transactions wait for each other forever. The wait-for graph has an edge Ti→TjT_i \to T_j when TiT_i waits for a lock held by TjT_j; 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
step1234
T1
T2
locks held
waits for

Step 0

T1 writes A then B, T2 writes B then A.

0 / 4
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

T1T_1 holds AA and wants BB; T2T_2 holds BB and wants AA. 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
step1234
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.

0 / 4
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

Both transactions hold shared locks on AA 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

X1(A) w1(A) X2(A) …\text{X}_1(A)\ w_1(A)\ \text{X}_2(A)\ \dots: T2T_2's request is incompatible with T1T_1's exclusive lock, so T2T_2 must wait; the sequence is not a legal lock schedule until T1T_1 releases AA. S1(A) r1(A) u1(A) S1(B)\text{S}_1(A)\ r_1(A)\ u_1(A)\ \text{S}_1(B): T1T_1 locks BB after releasing AA: not two-phase.

EX 2Lock point order

Medium

For the basic 2PL sample X1(A) w1(A) X1(B) u1(A) S2(A) r2(A) …\text{X}_1(A)\ w_1(A)\ \text{X}_1(B)\ u_1(A)\ \text{S}_2(A)\ r_2(A)\ \dots: the lock point of T1T_1 is its lock of BB (third operation), that of T2T_2 is its lock of AA (fifth). Order T1 T2T_1\,T_2. The precedence graph has the single edge T1→T2T_1 \to T_2 (w1(A)w_1(A) before r2(A)r_2(A)): consistent.

EX 3Deadlock

Medium

T1T_1: w(A) w(B)w(A)\ w(B) and T2T_2: w(B) w(A)w(B)\ w(A), interleaved as w1(A) w2(B) w1(B) w2(A)w_1(A)\ w_2(B)\ w_1(B)\ w_2(A): after the first two operations each holds one lock; T1T_1 waits for T2T_2 and T2T_2 for T1T_1: wait-for cycle T1→T2→T1T_1 \to T_2 \to T_1. If both took the locks in the same order (AA then BB) 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 ⇒\Rightarrow 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

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

GATE 2005 · MCQ · 2 marksQ36 · page 411 of the book
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
GATE 2024 · Set 2 · MSQ · 2 marksQ5 · page 433 of the book
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
GATE 2019 · MCQ · 1 markQ12 · page 434 of the book
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
GATE 2016 · Set 1 · MCQ · 2 marksQ18 · page 436 of the book
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
GATE 2010 · MCQ · 1 markQ28 · page 438 of the book
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
GATE 2007 · MCQ · 2 marksQ33 · page 439 of the book
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

T1T_1: S(A) r(A) X(B) w(B) u(A) u(B)\text{S}(A)\ r(A)\ \text{X}(B)\ w(B)\ u(A)\ u(B). Is T1T_1 two-phase?

Show answer

Yes: both locks are acquired before the first unlock. Its lock point is the lock of BB.

Question 2

Which two-phase variant guarantees that no transaction ever waits while holding a lock?

Show answer

Conservative 2PL (all locks are acquired before the transaction starts).

Question 3

Why can a schedule of basic 2PL transactions be non-recoverable?

Show answer

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 ⇒\Rightarrow 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