NIMCET, GATE, CUET & CBSE test series are live — start practicing free
syllabuzAI

Two-Phase Locking and Timestamp Ordering | GATE CSE

Learn shared and exclusive locks, two-phase locking and its strict, rigorous and conservative variants, and basic timestamp ordering with the Thomas write rule for GATE CSE.

8 min read

AS

Aman Singh

Updated 26 November 2026

On this page

Quick answer: Concurrency control protocols let transactions run in parallel while only allowing serializable schedules. In lock-based control, a transaction takes a shared (S) lock to read and an exclusive (X) lock to write, and only S with S is compatible. Two-phase locking (2PL) says a transaction may not acquire any lock after its first unlock. 2PL guarantees conflict serializability (in lock-point order) but still allows deadlock and cascading rollback. Strict 2PL holds X locks until commit, rigorous 2PL holds all locks until commit, and conservative 2PL takes every lock up front, which avoids deadlock. Timestamp ordering never makes transactions wait, so it is deadlock free, and the Thomas write rule ignores obsolete writes to allow some view-serializable schedules.

GATE asks whether a schedule or transaction follows 2PL, what a timestamp protocol does on a given operation, and which variant guarantees which property. This topic is part of the Transactions and Concurrency Control chapter.

Locks

A lock is a variable attached to a data item that records how the item may be used right now. The lock manager grants or denies each lock request.

LockAllowsWho can hold it
Shared, S(A)Read onlyMany transactions at once
Exclusive, X(A)Read and writeOne transaction, with no other S or X lock on A

Lock compatibility matrix

Requested ↓ / Held →SX
SYesNo
XNoNo

Locking alone does not guarantee serializability. A transaction that unlocks A and later locks B can still create a non-serializable interleaving, so a locking protocol is needed.

Two-phase locking (2PL)

A transaction may request locks only until its first unlock. It runs in two phases:

  • Growing phase: acquire locks, release none. The point where the last lock is acquired is the lock point.
  • Shrinking phase: release locks, acquire none.

Follows 2PL: S(A), X(B), S(C), X(D), U(C), U(D), U(A), U(B). The lock point is just after X(D).

Violates 2PL: X(A), R(A), W(A), U(A), X(B), … because X(B) comes after the first unlock U(A).

What 2PL guarantees

  • Every schedule produced by 2PL is conflict serializable.
  • The equivalent serial order is the order of the transactions' lock points.
  • If a schedule's precedence graph has a cycle, 2PL cannot produce it.
  • 2PL does not allow every conflict-serializable schedule. The set of 2PL schedules is a proper subset of conflict-serializable schedules.

Limitations of basic 2PL

  • Deadlock is possible. T1 holds A and waits for B while T2 holds B and waits for A.
  • Starvation is possible.
  • It does not prevent irrecoverable schedules or cascading rollbacks, because locks can be released before commit.

Variants of 2PL

VariantExtra rule on top of basic 2PL
Strict 2PLHold all exclusive locks until commit or abort
Rigorous 2PLHold all locks (S and X) until commit or abort
Conservative (static) 2PLAcquire every needed lock before the transaction starts; if any is unavailable, take none and wait

Summary table: 2PL variants

PropertyBasic 2PLStrict 2PLRigorous 2PLConservative 2PL
Conflict serializableYesYesYesYes
Strict (cascadeless) schedulesNot guaranteedYesYesNot guaranteed
Deadlock freeNoNoNoYes
Starvation freeNoNoNoNo
Serial orderLock pointsLock pointsCommit orderLock points

Deadlock handling with timestamps

Two classic prevention schemes use transaction timestamps (older = smaller timestamp):

  • Wait-die: an older requester waits for a younger holder; a younger requester dies (aborts and restarts with its original timestamp).
  • Wound-wait: an older requester wounds (aborts) a younger holder; a younger requester waits.

Because a restarted transaction keeps its original timestamp, it eventually becomes the oldest, so neither scheme starves a transaction forever.

Timestamp ordering (TO) protocols

Each transaction T gets a unique timestamp TS(T) when it starts. Older transactions have smaller timestamps. Each data item A keeps:

  • RTS(A): the largest timestamp of any transaction that has successfully read A.
  • WTS(A): the largest timestamp of any transaction that has successfully written A.

Basic timestamp ordering

When T issues R(A):

  • If WTS(A) > TS(T), roll back T, because a younger transaction has already written A.
  • Otherwise, run the read and set RTS(A) = max(RTS(A), TS(T)).

When T issues W(A):

  • If RTS(A) > TS(T), roll back T, because a younger transaction has already read A.
  • Else if WTS(A) > TS(T), roll back T.
  • Otherwise, run the write and set WTS(A) = TS(T).

Thomas write rule

This is basic TO with one change in the write check. If WTS(A) > TS(T), the write is ignored instead of rolling T back, because a younger transaction's write has already replaced it. This allows some view-serializable schedules that are not conflict serializable.

Strict timestamp ordering

Strict TO adds one rule: a transaction that wants to read or write A must wait until the transaction that last wrote A commits or aborts. This gives strict schedules.

Summary table: timestamp protocols

PropertyBasic TOThomas write ruleStrict TO
SerializabilityConflictViewConflict
Deadlock freeYesYesYes
Starvation freeNoNoNo
Strict schedulesNoNoYes

Worked examples

Assume TS(T1) = 10, TS(T2) = 20, and every RTS and WTS starts at 0.

Example 1: R1(A), W2(A), W1(A)

Step 1: R1(A). WTS(A) = 0 ≤ 10, so the read runs and RTS(A) becomes 10.

Step 2: W2(A). RTS(A) = 10 ≤ 20 and WTS(A) = 0 ≤ 20, so the write runs and WTS(A) becomes 20.

Step 3: W1(A). RTS(A) = 10 is not greater than 10, but WTS(A) = 20 > 10.

  • Basic TO: roll back T1.
  • Thomas write rule: ignore W1(A) and carry on.

Example 2: R2(A), W1(A)

Step 1: R2(A) runs and RTS(A) becomes 20.

Step 2: W1(A). RTS(A) = 20 > 10, so T1 is rolled back under both basic TO and the Thomas write rule.

Cheat sheet

ProtocolKey ruleMain guarantee
2PLNo lock after the first unlockConflict serializable (lock-point order)
Strict 2PLHold X locks until commitAlso strict schedules
Rigorous 2PLHold all locks until commitAlso strict; serial in commit order
Conservative 2PLLock everything firstAlso deadlock free
Basic TORoll back late operationsConflict serializable, deadlock free
Thomas write ruleIgnore obsolete writesView serializable

Exam tips

  • To check 2PL, find each transaction's first unlock and make sure no lock request comes after it.
  • For timestamp questions, update RTS and WTS after every successful operation and write the values down; most mistakes come from stale values.
  • "Free from deadlock" in a 2PL question almost always points to conservative 2PL.

Common mistakes

  • Thinking 2PL prevents deadlock. Only conservative 2PL does.
  • Thinking basic 2PL gives recoverable schedules. You need strict or rigorous 2PL for that.
  • Applying the Thomas write rule to reads. It only changes the obsolete-write case.
  • Mixing up who waits in wait-die and wound-wait. In both, the older transaction is favoured.

Practice questions

Q1. Does the transaction S(A), R(A), X(B), U(A), W(B), U(B) follow 2PL?

Answer: Yes. Both locks, S(A) and X(B), are acquired before the first unlock.

Q2. Which 2PL variant is free from deadlock?

Answer: Conservative (static) 2PL.

Q3. TS(T1) = 5 and TS(T2) = 8. T2 has written A, so WTS(A) = 8, and now T1 issues R(A). What happens under basic TO?

Answer: WTS(A) = 8 > 5, so T1 is rolled back.

FAQs

Does two-phase locking guarantee serializability?

Yes. Any schedule produced by 2PL is conflict serializable, and the serial order follows the transactions' lock points.

What is the difference between strict 2PL and rigorous 2PL?

Strict 2PL holds only exclusive locks until commit. Rigorous 2PL holds both shared and exclusive locks until commit, so transactions serialize in commit order.

Can deadlock happen under 2PL?

Yes, under basic, strict and rigorous 2PL. Conservative 2PL avoids it by taking all locks before the transaction starts.

Is timestamp ordering free from deadlock?

Yes. Transactions never wait for each other in basic timestamp ordering. A conflict is resolved by rolling back the transaction whose operation arrived too late.

What does the Thomas write rule do?

When a transaction tries to write an item that a younger transaction has already written, the old write is skipped instead of rolling the transaction back.

Conclusion

For locking questions, find each transaction's first unlock and check that no lock follows it. For timestamp questions, track RTS and WTS operation by operation and apply the rules. Go back to the Transactions and Concurrency Control chapter, revise recoverable schedules, or review serializability.

Concept Check

MEDIUM

Consider the following schedules involving two transactions. Which one of the following statements is TRUE?

S1S_1: r1(X);r1(Y);r2(X);r2(Y);w2(Y);w1(X)r_1(X); r_1(Y); r_2(X); r_2(Y); w_2(Y); w_1(X) S2S_2: r1(X);r2(X);r2(Y);w2(Y);r1(Y);w1(X)r_1(X); r_2(X); r_2(Y); w_2(Y); r_1(Y); w_1(X)

Suggested for you