Two-phase locking improves on Explicit Locks


Last Updated on Feb 26, 2021

Two-phase locking is similar to Explicit Locks but makes the lock requirements much stronger to guarantee serial execution. Unlike Snapshot Isolation, which blocks other write transactions, both read and write transactions have to acquire locks in 2PL.

The blocking of readers and writers is accomplished via locks on objects. The lock can either be in shared mode or exclusive mode.

  • A lock in shared mode is necessary for a transaction to read an object. Several transactions can hold the lock in shared mode simultaneously. But if another transaction already has an exclusive lock on the object, all other transactions have to wait.
  • If a transaction wants to write to an object, it has to request an exclusive lock. No other transaction can hold the lock simultaneously (shared or exclusive), so all transactions have to wait once an object is locked.
  • A transaction can start in shared mode and upgrade later to exclusive mode to write to the object. The upgrading process is the same as acquiring a new exclusive lock.
  • A transaction with an exclusive lock must continue to hold the lock until the end (commit or abort).

© 2022 Ambitious Systems. All Rights Reserved.