Conservative 2-Phase Locking
The article aims to explain to you about the Conservative 2-Phase Locking. First, we will learn about concurrency control. After that, we will learn briefly about different types of Concurrency protocols. After that, we will look into Two-Phase Concurrency Protocols. And in succession, we will be seeing what Conservative 2-Phase Locking is.
DBMS can be defined as the procedure of managing simultaneous operations without conflicting with one another. It ensures that Database transactions are performed concurrently and accurately to supply correct results without violating the data integrity of the respective Database.
DBMS Concurrency Control deals with such conflicts, mostly occurring with a multi-user system. Therefore, Concurrency Control is the most vital element for a correct functioning of a management system where two or more database transactions are executed simultaneously, which require access to comparable data.
Concurrency control protocols
Different concurrency control protocols offer additional benefits between the quantity of Concurrency they permit and, therefore, the amount of overhead they impose.
The Concurrency Control techniques in DBMS are:
- Timestamp-Based Protocols
- Lock-Based Protocols
1) Basic - 2PL
2) Conservative - 2PL
3) Strict - 2PL
4) Rigorous - 2PL
In the Lock-Based Protocols, a transaction cannot read or write data until it acquires an appropriate lock.
The two types of Locks are:
- Shared Lock: It is also known as read lock. It is only used for reading the data items. It is denoted as Lock-S.
- Exclusive Lock: A data item is often read and written with the Exclusive Lock. It can be owned by just one transaction at a time. It is denoted as Lock-X.
Two-Phase Locking Protocol
If locking and unlocking are done in two steps, a transaction is considered to follow the Two-Phase Locking Protocol. The two phases are:
- New locks on data items may be gained during the growing phase, but none can be released.
- Existing locks may be relinquished, but no new locks can be obtained during the shrinking phase.
Lock Point: Lock Point is when the Growing Phase ends.
It is also known as the Static-2PL. By pre-declaring its read-set and write-set, this protocol compels the transaction to lock all the items it accesses before it begins execution. None of the items are locked by the transaction if any pre-declared objects cannot be locked. Instead, it waits until all of the items are ready to be locked before proceeding. There is no deadlock in the conservative 2-PL, but it does not ensure a Strict schedule. However, it isn't easy to use in practice because of the need to pre-declare the read-set and the write-set, which is impossible in various situations.
- Following this schedule, unlike Basic, Strict, and Rigorous 2-PL, there will be no Growing Phase. This protocol does not have a Growing phase since locking data before utilizing it is required. Furthermore, this rule eliminates deadlock by releasing all locks and attempting again later if an item is not accessible for locking, i.e., no Hold and Wait. As a result, one of the four needed requirements for deadlock is no longer met.
- We need to lock all products ahead of time, so there are no limits on releasing or unlocking them, as there were in Strict or Rigorous 2-PL.
- We have no Growing phase in this protocol, unlike Basic, Strict, and Rigorous 2-PL, because no operations are performed until acquiring all of the locks.
- Although this protocol provides a Deadlock-free schedule, it may have downsides such as Cascading Rollbacks. As a result, this protocol does not guarantee Strict Schedules. In comparison to Strict and Rigorous 2-PL, this is a disadvantage.
Let us get a better understanding of Conservative two-phase locking with the help of an example:
When the lock on A is released in the schedule, it is possible for transaction T2 to acquire the lock on A. Now, transaction T2 reads the value of A that transaction T1 has written. This means that if transaction T1 fails, transaction T2 must also fail for consistency reasons.
Frequently asked questions
- Is conservative two-phase locking Strict?
Conservative 2-PL avoids deadlock, but it does not guarantee a strict timeline. However, it's challenging to utilize in practice because it requires pre-declaring the read-set and write-set, which isn't always possible.
- Is conservative 2PL a recoverable schedule?
Yes, conservative 2PL is recoverable, serializable, deadlock-free, but still, it suffers from starvation.
- How does 2PL ensure that no deadlock occurs?
The protocol employs locks applied to data by a transaction and can prevent other transactions from accessing the same data throughout the transaction's lifetime.
Locks are applied and withdrawn in two steps according to the 2PL protocol:
1) Locks are obtained, and no locks are released during the expanding phase.
2) Locks are released, and no locks are acquired during the shrinking step.
The article taught you about the Concurrency Control Protocols, their Potential problems, and their advantages. Then we saw different types of concurrency control protocols where we learned in detail about the Lock Based Protocols. In Lock Based Protocols, we saw its different types and learned about the Conservative-2PL n expansion.
Ninjas! You have taken the first yet most crucial step in learning about Concurrency Control Protocols.
Please do not stop learning here and master the most required topics that may help you ace the placement interviews by enrolling in our courses. You can practice some questions from our Top 100 SQL problems on Codestudio to improve your knowledge of DBMS.