Locking technique, Its Working, 2-phase locking and Comparison

The locking technique uses a lock that guarantees the exclusive use of data of transactions. The lock is used to restrict different transaction which is executing at the same time.

There are 3 phases of Locking technique

  1. Taking the lock control and accessing the data of the transaction
  2. Complete the transaction execution
  3. Release lock

There are two types of lock available in this protocol

  1. Shared lock: It is also represented by ‘lock-S’, and allows only to read the data from the transaction.
  2. Exclusive lock: It is also represented by ‘lock-X, and allows both the read and write operation to the data from the transaction.

2-phase locking technique

It is the same as the Locking technique explained above but it has two phases to act which are:

  1. Growing phase: It is a phase where a transaction acquires the lock but does not release it.
  2. Shrinking phase: It is a phase where a transaction releases the lock but does not acquire it.

The 2-phase locking technique is further divided into 3 categories

  1. Strict 2 phase locking
  2. Rigorous 2 phase locking
  3. Conservative 2 phase locking

Strict 2 phase locking

This locking technique satisfies the basic 2 phase locking but holds all the exclusive locks until the transaction is committed or aborted during the execution.

Rigorous 2 phase locking

This locking technique satisfies the basic 2 phase locking but holds all the exclusive locks and shared locks until the transaction is committed or aborted during the execution.

Conservative 2 phase locking

It is a locking technique that satisfies the basic 2 phase locking but it holds all the required locks at the beginning of transaction execution.

Rigorous two-phase locking is stricter than any of the available categories but it is very easy to implement. It ensures Cascadeless and strict recovery.

Rigorous 2 phase lockingStrict 2 phase lockingConservative 2 phase locking
It locks the exclusive and shared locks until the transaction gets committed or abortedIt locks the exclusive locks only until the transaction gets committed or abortedIt acquires all the locks required for the transaction at the beginning of transaction execution.
It is easy to implement.It is complex to implement.It is not practically not possible.
It does not have any growing phase but has shrinking phase.It does not have any growing phase but has shrinking phase.It may have any growing phase but no shrinking phase
It is not deadlock-free.It is not deadlock-free.It is deadlock-free.
It is stricter than strict and conservative 2 phase lockingIt is stricter than basic locking techniques.It is practically not possible.