What is two-phase locking (2PL), and what is strict 2PL?
MediumConceptConcurrency Control & Locking
Answer
Two-phase locking (2PL) is a locking protocol that guarantees conflict-serializable schedules. Every transaction works in two phases:
- Growing phase: it may acquire locks but not release any.
- Shrinking phase: after it releases its first lock, it may not acquire any new lock. The point of its last lock acquisition is its lock point, and transactions are serialized in the order of their lock points.
- Basic 2PL can still produce cascading rollbacks and deadlocks.
- Strict 2PL holds all exclusive locks until commit or abort, which prevents cascading rollbacks; rigorous 2PL holds every lock until the end. Lock-based engines such as SQL Server and MySQL InnoDB hold write locks until commit, as strict 2PL requires.
Example: A transfer from A to B:
T1: lock-X(A) read A, write A
T1: lock-X(B) read B, write B <- lock point (growing ends)
T1: unlock(A) unlock(B) <- shrinking phase
Under strict 2PL, T1 would release both locks only after COMMIT. A sequence such as lock(A), unlock(A), lock(B) violates 2PL.