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
lock-S(A);
read(A);
lock-S(B);
read(B);
display(A+B);
unlock(A);
unlock(B);
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.    

Comments

Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Conflict Serializability in database