Open In App

Two Phase Locking (2-PL) Concurrency Control Protocol | Set 3

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In database systems, managing concurrent transactions is essential to maintain data consistency and ensure smooth operation. Two-Phase Locking (2PL) is a widely used concurrency control protocol that enforces serializability ensuring transactions are executed in a conflict-free manner. By dividing a transaction into two distinct phases growing (acquiring locks) and shrinking (releasing locks), 2PL prevents data inconsistencies caused by overlapping reads and writes. 

Conservative 2-PL -

This protocol requires the transaction to lock all the items it accesses before the Transaction begins. Two-Phase Locking (2PL) is a method used in database systems to control how transactions access and modify data. It ensures that all transactions are performed in a safe and consistent way by following two simple steps:

  1. Growing Phase: A transaction first acquires all the locks it needs (both for reading and writing data). During this phase, no locks are released.
  2. Shrinking Phase: After acquiring all necessary locks the transaction starts releasing them one by one, but no new locks can be acquired.

By following these two phases, 2PL guarantees that transactions happen in a specific order avoiding problems like one transaction interfering with another’s work. This makes the database reliable and prevents errors such as dirty reads, lost updates or data inconsistencies.

Some interesting traits about Conservative 2-PL: 

  • Schedule following this will not have a Growing Phase as we've seen in Basic, Strict, and Rigorous 2-PL. As locking the data before using it is mandatory so this protocol has no Growing phase. Moreover, this rule makes it Deadlock free as if an item is not available for locking the transaction releases all the locks and tries again later, i.e. no Hold and Wait. This makes one of the four necessary conditions for deadlock void.
  • We only have to lock all the items beforehand so releasing or unlocking them has no restrictions as we had in Strict or Rigorous 2-PL.
  • As no operations are done before acquiring all the locks, we have no Growing phase in this protocol, unlike Basic, Strict, Rigorous 2-PL.
  • Although we get a Deadlock free schedule in this protocol, we may still face drawbacks like Cascading Rollbacks. So this protocol does not ensure Strict Schedules. This is a disadvantage in comparison to Strict and Rigorous 2-PL.


Let's discuss an example now. See how the schedule below follows Conservative 2-PL but does not follow Strict and Rigorous 2-PL.  

 T1T2
1 
 
Lock-X(A) 
2Lock-X(B) 
3Read(A) 
4*operation on A 
5Write(A) 
6Unlock(A) 
7 Lock-X(A)
8 Read(A)
9 *operation on A
10 Write(A)
11 Unlock(A)
12Read(B) 
13*operation on B 
14Write(B) 
15Unlock(B) 
16Commit 
17 Commit

This example demonstrates Conservative Two-Phase Locking (2PL) because both transactions T1 and T2 acquire all their required locks at the beginning of their execution, but fails to meet the requirements of Strict and Rigorous 2-PL, that is because we unlock A and B before the transaction commits.

How can cascading abort happen in Conservative 2-PL? 

Cascading aborts in Conservative 2-PL happen when one transaction depends on uncommitted changes made by another transaction, and the first transaction is later aborted. For example, if Transaction T2​ reads data written by T1 and T1​ aborts before committing, the data used by T2​ becomes invalid. As a result, T2​ must also abort to maintain consistency. This can trigger a chain reaction where multiple transactions relying on invalid data are aborted one after the other.

Although Conservative 2-PL minimizes the chances of such situations by requiring all locks to be acquired before the transaction starts, cascading aborts can still occur if transactions read uncommitted changes. Proper implementation of lock handling and ensuring that no uncommitted data is accessed is key to avoiding cascading aborts in such cases.  

Conclusion

Conservative Two-Phase Locking (2PL) is a simple and effective way to handle multiple transactions in a database without causing conflicts or errors. It prevents deadlocks by making sure that all locks are acquired before a transaction starts. While it may cause some waiting, it ensures transactions are safe and consistent, keeping the database accurate and reliable. This makes it a useful approach for handling concurrency in database systems.

GATE related question: 
GATE-CS-2016 (Set 1) | Question 61
 


Next Article
Article Tags :
Practice Tags :

Similar Reads