Introduction
Before getting started, let us learn about lock-based protocols in the database management system.
You can also read about - Specialization and Generalization in DBMS and Checkpoint in DBMS
Lock-based protocols
In lock-based protocols, a transaction can not read and write the data until it acquires an appropriate lock. There are two types of locks that are as follows:
- Shared lock
Also known as Read-only lock, data items can only be read by the transaction in this lock.
It can be shared between the transactions themselves because it can not update the item's data when the transaction holds a lock.
- Exclusive lock
In this lock, transactions read and write the data item.
As the name suggests, this lock is exclusive, and here multiple transactions do not modify the same data simultaneously.
Locking and unlocking should be done in such a way that there will be no inconsistency, deadlock and there will be no starvation.
There are three standard lock protocols available that are:
- Simplistic lock protocol
- Pre-claiming lock protocol
- Two-phase locking
In this blog, we will learn about two-phase locking in detail.
Two-phase locking (2PL)
- The two-phase locking protocol divides the execution phase of the transaction into three parts. These parts are given below.
- First part: when the execution of the transaction begins, it seeks permission from the lock it requires.
- Second part: when the transaction acquires all the locks, the third part starts as soon as the transaction releases the first lock.
- Third part: The transaction cannot demand any new locks at that phase; it can only release the acquired locks.
There are two phases of two-phase locking:
Growing phase:
The issuing of all the locks is done in this phase. In this, a new lock may be acquired by the transaction on the data item, but none can be released.
Shrinking phase:
No locks are issued in this phase, and all the changes to the data items are stored; later on, locks are released.
Example:
Let us take an example to understand this clearly. Suppose there are two transactions, T1 and T2.
Steps |
Transaction T1 |
Transaction T2 |
0 |
Lock–S(A) |
|
1 |
Lock–S(A) |
|
2 |
Lock–X(B) |
|
3 |
— |
— |
4 |
Unlock(A) |
|
5 |
Lock–X(C) |
|
6 |
Unlock(B) |
|
7 |
Unlock(A) |
|
8 |
Unlock(C) |
|
9 |
— |
— |
Here,
Lock–S(A): can’t execute Lock–S(A), since A is locked by transaction T1.
Lock–X(C): can’t execute Lock–X(C), since C is locked by transaction T2.
And, Unlock(A) means unlocking of A.
The same goes for S(A) and S(B).
In the above example, the following phase may happen if the lock conversion is allowed:
- Upgrading of the lock is allowed in the growing phase( from S(A) to X(A) ).
- Downgrading of the lock must be done in the shrinking phase( from X(A) to S(A) ).
The following way shows how locking and unlocking happen in two-phase locking(2PL).
In transaction T1:
- Growing phase: from steps 1 to 3
- Shrinking phase: from steps 5 to 7
- Lock point: at step 3
In transaction T2:
- Growing phase: from steps 2 to 6
- Shrinking phase: from steps 8 to 9
- Lock point: at step 6
Following are the most common types of Two-phase locking protocol:
- Strict two-phase locking protocols
- Rigorous two-phase locking protocols
- Conservative two-phase locking protocols
Strict two-phase locking protocols
- The first phase of strict two-phase locking is similar to the two-phase locking. The transaction continues to execute normally after acquiring all the locks.
- The only difference between strict 2PL and 2PL is that strict two-phase locking does not release a lock after using it. On the other hand, it waits for the whole transaction to complete, and then it releases all the locks.
Rigorous two-phase locking protocols
- Rigorous two-phase locking protocols avoid cascading rollbacks.
- In this, all the shared and exclusive locks are only released when the whole transaction completes.
Conservative two-phase locking protocols
- Conservative two-phase locking protocols are used in relational databases.
- This method of locking prevents deadlock but does not prevent starvation and cascading rollback.
- All locks are acquired before the beginning of the transaction no acquisition in-between transactions, therefore, no deadlock but the rollback problem still is there.
Example:
The two-phase locking guarantees serializability, but it can’t guarantee that deadlock will not happen in this case.
Let us understand this using an example.
Let there be two transactions, T1 and T2, where
T1=A+B and T2=B+A
Transaction T1 |
Transaction T2 |
Lock–X(A) |
Lock–X(B) |
Read A; |
Read B; |
Lock–X(B) |
Lock–X(A) |
Here,
Lock–X(A): can’t execute Lock–X(A), since B is locked by transaction T1.
Lock–X(B): can’t execute Lock–X(B), since B is locked by transaction T2.
In the above example, Transaction T1 waits for B and T2 waits for A, and this waiting time never ends. Both the transactions cannot proceed further until at least one releases the lock voluntarily. This situation is called deadlock.
Recommended Topic, B+ Tree in DBMS and Recursive Relationship in DBMS
Frequently Asked Questions
-
What do you mean by lock point?
In the growing phase, transactions reach a point where all the locks are acquired that are needed. This point is known as the Lock point. The transaction enters the shrinking phase after the lock point is reached.
-
What is the cascading schedule?
In this schedule, one transaction depends on the other transaction, so if one needs to roll back, the other has to roll back too.
-
What is a deadlock?
In a database, deadlock is an unwanted situation in which two or more transactions are waiting indefinitely for one another to give up locks.