Sub topic 10
Two-Phase Locking and Its Strict and Rigorous Variants
Assume you know basic SQL, transactions, shared and exclusive locks, and conflict serializability. This chapter uses S for a shared lock, X for an exclusive lock, and C for commit.
Two-phase locking (2PL) is a concurrency-control protocol. It controls when a transaction may acquire and release locks. Its main guarantee is conflict serializability: every schedule produced by 2PL is conflict-equivalent to some serial schedule.
The two phases
A transaction using basic 2PL has two phases:
- Growing phase: the transaction may acquire locks, but it may not release any lock.
- Shrinking phase: the transaction may release locks, but it may not acquire any new lock.
The point where the transaction obtains its final lock is called its lock point. The serial order of transactions follows their lock points.
For example, suppose T1 locks A and then B. After T1 unlocks A, it cannot lock C. Releasing A has moved T1 into its shrinking phase.
Basic 2PL does not require locks to remain held until commit. Therefore, a transaction can release a lock, another transaction can read the changed item, and the first transaction can later abort. This may cause cascading rollback.
Strict and rigorous 2PL
Strict 2PL strengthens basic 2PL by holding every exclusive lock until the transaction commits or aborts. Other transactions cannot read or overwrite data changed by an unfinished transaction.
Rigorous 2PL is stronger still. It holds both shared and exclusive locks until commit or abort. The transaction releases all locks at the end.
| Protocol | Locks held until commit or abort | Main guarantee |
|---|---|---|
| Basic 2PL | No required lock type | Conflict serializability |
| Strict 2PL | Exclusive locks | Conflict serializability and no dirty reads or writes |
| Rigorous 2PL | Shared and exclusive locks | Strict schedules and a clear commit order |
A strict schedule prevents cascading rollback because no transaction can use data written by an uncommitted transaction. Rigorous 2PL also prevents another transaction from acquiring a shared lock on data still read by an uncommitted transaction.
Worked schedule
Consider this schedule:
T1: X-lock(A), write(A), X-lock(B), write(B), commit, unlock(A), unlock(B)
T2: waits for X-lock(B), then writes(B), commitT1 acquires locks during its growing phase and keeps them until commit. T2 cannot write B until T1 commits and releases B. The result is equivalent to T1 running before T2.
With basic 2PL, T1 could unlock B before committing. T2 might then write B while T1 is still active. If T1 aborts, T2 may need to roll back. Strict 2PL avoids this situation by delaying T2.
Applying the protocol
When analyzing a schedule:
- Mark each lock acquisition and release.
- Check that no transaction acquires a lock after its first unlock.
- For strict 2PL, check that each exclusive lock lasts through commit or abort.
- For rigorous 2PL, check the same condition for every lock.
- If a requested lock conflicts with a held lock, make the requester wait.
Exercise
T1 holds an X-lock on A and has not committed. T2 requests an S-lock on A. Under strict 2PL, may T2 proceed?
Show answer
No. T2 must wait because strict 2PL holds T1's exclusive lock until T1 commits or aborts.