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.
| Lock | Allows | Who can hold it |
|---|---|---|
| Shared, S(A) | Read only | Many transactions at once |
| Exclusive, X(A) | Read and write | One transaction, with no other S or X lock on A |
Lock compatibility matrix
| Requested ↓ / Held → | S | X |
|---|---|---|
| S | Yes | No |
| X | No | No |
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
| Variant | Extra rule on top of basic 2PL |
|---|---|
| Strict 2PL | Hold all exclusive locks until commit or abort |
| Rigorous 2PL | Hold all locks (S and X) until commit or abort |
| Conservative (static) 2PL | Acquire every needed lock before the transaction starts; if any is unavailable, take none and wait |
Summary table: 2PL variants
| Property | Basic 2PL | Strict 2PL | Rigorous 2PL | Conservative 2PL |
|---|---|---|---|---|
| Conflict serializable | Yes | Yes | Yes | Yes |
| Strict (cascadeless) schedules | Not guaranteed | Yes | Yes | Not guaranteed |
| Deadlock free | No | No | No | Yes |
| Starvation free | No | No | No | No |
| Serial order | Lock points | Lock points | Commit order | Lock 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
| Property | Basic TO | Thomas write rule | Strict TO |
|---|---|---|---|
| Serializability | Conflict | View | Conflict |
| Deadlock free | Yes | Yes | Yes |
| Starvation free | No | No | No |
| Strict schedules | No | No | Yes |
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
| Protocol | Key rule | Main guarantee |
|---|---|---|
| 2PL | No lock after the first unlock | Conflict serializable (lock-point order) |
| Strict 2PL | Hold X locks until commit | Also strict schedules |
| Rigorous 2PL | Hold all locks until commit | Also strict; serial in commit order |
| Conservative 2PL | Lock everything first | Also deadlock free |
| Basic TO | Roll back late operations | Conflict serializable, deadlock free |
| Thomas write rule | Ignore obsolete writes | View 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.