Two-Phase Locking in Databases

What is Two-Phase Locking (2PL)?

  • Two-Phase Locking (2PL) is a concurrency control method which divides the execution phase of a transaction into three parts.
  • It ensures conflict serializable schedules.
  • If read and write operations introduce the first unlock operation in the transaction, then it is said to be Two-Phase Locking Protocol.

  • This protocol can be divided into two phases,
    1. In Growing Phase, a transaction obtains locks, but may not release any lock.
    2. In Shrinking Phase, a transaction may release locks, but may not obtain any lock.

  • Two-Phase Locking does not ensure freedom from deadlocks.

Types of Two – Phase Locking Protocol

Following are the types of two – phase locking protocol:

1. Strict Two – Phase Locking Protocol
2. Rigorous Two – Phase Locking Protocol
3. Conservative Two – Phase Locking Protocol

1. Strict Two-Phase Locking Protocol
  • Strict Two-Phase Locking Protocol avoids cascaded rollbacks.
  • This protocol not only requires two-phase locking but also all exclusive-locks should be held until the transaction commits or aborts.
  • It is not deadlock free.
  • It ensures that if data is being modified by one transaction, then other transaction cannot read it until first transaction commits.
  • Most of the database systems implement rigorous two – phase locking protocol.
2. Rigorous Two-Phase Locking
  • Rigorous Two – Phase Locking Protocol avoids cascading rollbacks.
  • This protocol requires that all the share and exclusive locks to be held until the transaction commits.
3. Conservative Two-Phase Locking Protocol
  • Conservative Two – Phase Locking Protocol is also called as Static Two – Phase Locking Protocol.
  • This protocol is almost free from deadlocks as all required items are listed in advanced.
  • It requires locking of all data items to access before the transaction starts.