Friday, 23 March 2012

Two-Phase Locking Protocol

To maintain serializability we use two-phase locking protocol in database transaction. Specially we use the following two protocol for data locking purpose. We divide locking protocol into two phase. First phase acquire locks and second phase release the locks. Given below the two-phase locking protocol.
1. Growing phase: A transaction firstly acquired locks, but may not release any lock. 
2. Shrinking phase:  A transaction may release locks, but at the time not taking any new locks.

Initially, in the growing phase a transaction acquires locks as needed. Once the transaction start to release locks, it enters the shrinking phase. Now consider this transaction
In the above transaction at first it acquire locks serially and then it releases the locks. This transaction follow two phase locking protocol.Two-Phase locking protocol ensures conflict serializability. But unfortunately locking can lead to an undesirable situation. Now consider the partial schedule in this figure.
T3 is holding an exclusive-mode lock on B and T4 is requesting a shared-mode lock on B, T4 is waiting for T3 to unlock B. Similarly, T4 is holding a shared-mode lock on A and T3 is requesting an exclusive-mode lock on A, T3 is waiting for T4 to unlock A. In this situation anyone can't proceed and it makes a deadlock situation. When deadlock occurs the system must roll back. I discuss deadlock handling procedure in next tutorial. I hope you will enjoy this.    


//infolinks add placement