Sub topic 10
Two-Phase Locking and Its Strict and Rigorous Variants
This chapter assumes you know the following:
- Shared (S) and exclusive (X) locks, and the compatibility matrix.
- Precedence graphs and conflict serializability.
- The definitions of recoverable, cascadeless and strict schedules.
Why locking alone is not enough
Taking a lock before each access does not make a schedule correct. Suppose each transaction locks an item, uses it, and unlocks it right away. Take T1, which updates A and then B, and T2, which updates A and then B.
- T1 locks A, writes A, unlocks A.
- T2 locks A, writes A, unlocks A. T2 then locks B, writes B, unlocks B, and commits.
- T1 locks B, writes B, unlocks B, and commits.
Every access was protected by a lock. The conflicts still give T1 → T2 on A and T2 → T1 on B, so the precedence graph has a cycle. The schedule is not conflict serializable.
The error is in when T1 released A. It gave up a lock and then asked for a new one. Two-phase locking forbids exactly that.
Basic two-phase locking
Under the two-phase locking (2PL) protocol, every transaction has two phases:
- Growing phase: the transaction may acquire locks. It may not release any.
- Shrinking phase: the transaction may release locks. It may not acquire any.
The lock point is the moment a transaction holds its last lock, which is the end of the growing phase. A lock upgrade (S to X) counts as acquiring, so it is allowed only while growing. A downgrade (X to S) counts as releasing, so it is allowed only while shrinking.
What basic 2PL guarantees
Every schedule that obeys 2PL is conflict serializable. The equivalent serial order is the order of the transactions' lock points.
The argument is short. Suppose Ti has a conflict edge to Tj on item Q. Ti released Q before Tj acquired it. Ti's lock point comes before its release of Q, and Tj's lock point comes after its acquisition of Q. So Ti's lock point is earlier than Tj's, and every edge in the precedence graph goes from an earlier lock point to a later one. A cycle is then impossible.
What basic 2PL does not guarantee
- Deadlock freedom. T1 holds X(A) and wants X(B). T2 holds X(B) and wants X(A). Both are in their growing phase and both wait forever.
- Recoverability. A transaction may release an X lock before it commits. Another transaction can then read the uncommitted value.
- Cascadeless or strict schedules. These fail for the same reason as recoverability.
- Every conflict serializable schedule. The set of 2PL schedules is a proper subset of the conflict serializable ones. Consider r1(A), w2(A), c2, w1(B). Its only edge is T1 → T2, so it is conflict serializable. To produce it under 2PL, T1 would have to hold its lock on A until it locked B. T2's write on A would then be blocked, so T2 could not run in the middle.
The recoverability problem
Take this schedule under basic 2PL:
- T1 locks A with X, writes A, then unlocks A. This is allowed because T1 has no more locks to acquire.
- T2 locks A, reads the value T1 wrote, and commits.
- T1 aborts.
T2 committed after reading data that never officially existed, so the schedule is not recoverable. If T2 had not committed, T2 would have to roll back too, which is a cascading rollback. The fix is to delay releasing locks until the end of the transaction.
Strict 2PL
Strict 2PL is basic 2PL with one added rule. A transaction holds all its exclusive locks until it commits or aborts. Shared locks may be released earlier, in the shrinking phase.
Because nobody can read or overwrite an uncommitted write, strict 2PL produces strict schedules. Strict schedules are also cascadeless and recoverable. Strict 2PL is also conflict serializable, because it is still 2PL.
Rigorous 2PL
Rigorous 2PL goes further. A transaction holds all its locks, shared and exclusive, until it commits or aborts. There is effectively no shrinking phase until the end, so the lock point and the release point are close together.
Rigorous 2PL gives everything strict 2PL does. It also gives a simpler serialization order: the commit order. If Ti has a conflict edge to Tj, Ti must have released its lock before Tj acquired it. Under rigorous 2PL, Ti releases only at its commit, so Ti commits first.
Many textbooks and some GATE questions call the rigorous variant "strict 2PL". Check which definition a question uses. If a question says "holds all locks until commit", it means rigorous.
| Protocol | When X locks are released | When S locks are released | Serial order | Strict schedules | Deadlock free |
|---|---|---|---|---|---|
| Basic 2PL | After lock point, any time | After lock point, any time | Lock point order | Not guaranteed | No |
| Strict 2PL | At commit or abort | After lock point, any time | Lock point order | Yes | No |
| Rigorous 2PL | At commit or abort | At commit or abort | Commit order | Yes | No |
| Conservative 2PL | Any time after start | Any time after start | Start order | Not guaranteed | Yes |
Conservative 2PL (static 2PL) acquires every lock before the transaction begins. If any lock is unavailable, it acquires none and waits. It cannot deadlock, because a transaction never waits while holding a lock. It is rarely used, because a transaction must know its whole lock set in advance. It also does not guarantee recoverability unless combined with the strict rule.
Containment
For the schedules each protocol can produce:
- Rigorous 2PL schedules are a subset of strict 2PL schedules.
- Strict 2PL schedules are a subset of basic 2PL schedules.
- Basic 2PL schedules are a subset of conflict serializable schedules.
Strict 2PL schedules are also a subset of strict schedules, and rigorous 2PL schedules are a subset of commit-ordered conflict serializable schedules.
Checking a lock trace
The following program takes a trace of lock operations for one transaction and says which variant it follows. Actions are S and X (acquire), U (unlock), and C (commit).
def classify(schedule):
# schedule: list of (txn, action, item)
shrinking = set()
committed = set()
modes = {}
two_phase = strict = rigorous = True
for t, a, x in schedule:
if a in ('S', 'X'):
if t in shrinking:
two_phase = False
modes[(t, x)] = a
elif a == 'U':
shrinking.add(t)
if t not in committed:
rigorous = False
if modes[(t, x)] == 'X':
strict = False
elif a == 'C':
committed.add(t)
if not two_phase:
return 'not 2PL'
if rigorous:
return 'rigorous 2PL'
if strict:
return 'strict 2PL'
return 'basic 2PL'
s1 = [('T1','X','A'), ('T1','X','B'), ('T1','U','A'), ('T1','U','B'), ('T1','C',None)]
s2 = [('T1','S','A'), ('T1','X','B'), ('T1','U','A'), ('T1','C',None), ('T1','U','B')]
s3 = [('T1','S','A'), ('T1','X','B'), ('T1','C',None), ('T1','U','A'), ('T1','U','B')]
s4 = [('T1','X','A'), ('T1','U','A'), ('T1','X','B'), ('T1','C',None), ('T1','U','B')]
for s in (s1, s2, s3, s4):
print(classify(s))The output is:
basic 2PL
strict 2PL
rigorous 2PL
not 2PLIn s1, T1 releases its X lock on A before commit, so it is only basic 2PL. In s2, the S lock on A is released before commit but the X lock on B is held until after commit, so it is strict but not rigorous. In s4, T1 locks B after unlocking A, which breaks the two-phase rule.
Locks in a real database
A database lock manager enforces strict 2PL implicitly. In SQL, SELECT ... FOR UPDATE takes an exclusive row lock, and the lock lasts until COMMIT or ROLLBACK. The second session in this example blocks until the first one finishes.
-- Session 1 -- Session 2
BEGIN; BEGIN;
SELECT balance FROM account SELECT balance FROM account
WHERE id = 1 FOR UPDATE; WHERE id = 1 FOR UPDATE; -- blocks here
UPDATE account SET balance = balance - 100
WHERE id = 1;
COMMIT; -- now proceeds and reads the committed balance
UPDATE account SET balance = balance - 50
WHERE id = 1;
COMMIT;No update is lost, and Session 2 never sees an uncommitted balance.
Common errors in GATE questions
- Assuming 2PL prevents deadlock. It does not, except in the conservative form. Deadlocks are handled separately with wait-for graphs, wait-die or wound-wait.
- Assuming 2PL implies recoverable. Only the strict and rigorous variants do. In a basic 2PL schedule, check whether an X lock is released before the writer commits.
- Assuming every conflict serializable schedule is 2PL. The converse fails, as shown earlier.
- Using the wrong serial order. For basic and strict 2PL, use the order of lock points. For rigorous 2PL, use the commit order. The precedence graph's topological order always gives a valid serial order when the schedule is conflict serializable.
- Treating lock upgrades as free. An upgrade is an acquisition, so it must happen before the first unlock. Two transactions that both hold S on the same item and both try to upgrade will deadlock.
Exercise
T1 holds S(A) and X(B). T1 releases S(A), then commits, then releases X(B). T1 takes no other locks after releasing S(A). Which is the strongest variant of 2PL this follows: basic, strict or rigorous?
Show answer
Strict 2PL. The S lock on A is released before commit, so it is not rigorous. The X lock on B is held until after commit, so the strict rule holds. No lock is acquired after the first unlock, so the two-phase rule holds.
Exercise
Under basic 2PL, T1 writes A, unlocks A, and then locks B (it already holds the lock it needs on C). Is this transaction following the two-phase rule? Explain.
Show answer
No. T1 unlocks A, which starts the shrinking phase, and then locks B, which is an acquisition. The two-phase rule forbids acquiring any lock after releasing one.
Exercise
A schedule is produced by rigorous 2PL. T1 has a conflict edge to T2 and T2 has a conflict edge to T3. In what order did the transactions commit, and why?
Show answer
T1, then T2, then T3. For each conflict edge Ti → Tj, Ti released its lock before Tj acquired it. Under rigorous 2PL, locks are released only at commit, so Ti committed before Tj. The serial order is therefore the commit order.