What is a two-phase locking protocol in DBMS?
With complex and huge amounts of data available, database management systems too have become more complex.
In a DBMS, multiple processes are carried out simultaneously. Hence, serializability needs to be maintained to ensure that no concurrent processes conflict with each other’s operations.
To maintain concurrent transactions without any conflict, multiple protocols are used. The very common type of protocol which is used is known as the lock-based protocol.
In such a protocol, the specified process will not perform any read or write operation until it acquires the desired lock.
One of the most-used lock-based protocols is the two-phase locking protocol in DBMS.
In this protocol, the lock is implemented in two phases to ensure the serialisation of conflicting schedules.
However, to implement the process, it is very important to understand the meaning of the 2 phase locking protocol in DBMS.
First, let’s begin with the basics.
What Is Two-phase Locking?
You can say that a transaction follows a two-phase locking protocol in case unlocking and locking can be performed in two phases. These phases include the growing phase in which new processes acquire locks but no older processes are released and the shrinking phase in which older locks are released but no new ones are acquired.
While working with 2 phase locking protocol in DBMS, you will often come across a term known as lock point. This is defined as the point where the growing phase starts ending. It simply means that at the lock point, the existing process has acquired its last desired lock and is ready to continue the work.
Types Of Locks
In locking protocols, you can implement two types of locks in the processes. These include
- Shared lock: When you apply a shared lock, the processes are only allowed to read. No process is allowed to write with the shared lock. This lock is represented as Lock-S.
- Exclusive Lock: When you apply an exclusive lock, processes are allowed to both read and write. This lock is represented as Lock-X.
Phases in Two-Phase Locking
As we have mentioned, a process is said to be in 2-phase locking in DBMS if the unlocking and locking process takes place in two phases.
The two phases of the process include;
- The Growing Phase: In this phase, a process is only allowed to acquire a lock. The process or transaction will not be able to release any lock in this phase. At this phase, when the changes in the data will be done, after that only the shrinking phase of the process will begin.
- Shrinking Phase: In this phase, none of the locks are released nor are obtained. When the data changes will be stored in the system, only then it will release locks.
Types Of Two-Phase Locking
Two-phase locking can be performed in the following three ways:
Rigorous Two-Phase Locking
- In this type of locking, the transaction will not be able to release any of the locks no matter if it is a shared lock or an exclusive lock.
- In rigorous locking, serializability is assured.
- However, you must know that this type of locking protocol does not guarantee deadlock.
Strict Two-Phase Locking
- When the transaction reaches the lock point, the transaction can choose to release the shared lock.
- However, the transaction can not release the exclusive lock until the whole transaction is completed.
- When it comes to 2-Phase locking in DBMS, in case one of the transactions rolls back, the other one should also roll back. Here, transactions may be Interdependent. This process is also known as cascading schedule.
Conservative Two-Phase Locking
- In this locking protocol, the transaction is required to lock every required data item it needs before starting the transaction.
- In case any data item is no more available, none of the required data items will be locked.
- The transaction is required to know all the required read and write data items before it begins. However, in practical cases, this is not possible.
- The conservative locking protocol does not follow a strict schedule.
- It is also called a deadlock-free protocol.
How does the 2 phase locking protocol in DBMS work?
The protocol will divide the whole process of executing the transaction into three components:
- In the first part of the transaction execution, the transaction will seek permission to acquire the required locks.
- In the second part of the transaction execution, the transaction will acquire all the locks it requires.
- Lastly, the last part of the transaction starts when the first acquired lock is released.
- However, the transaction will not be able to ask for any new lock in the last phase. All it does is release the locks that it has already acquired.
Now that we are discussing an important concept of DBMS, another important thing a programmer should understand is the anomalies in DBMS.
Anomalies in a database are generally referred to as discrepancies between different parts of the given database. Such inconsistency in a database may occur as a result of operations like insertion, updating or deletion.
Other than this, anomalies can be created if you have stored the same record in multiple tables or locations of the system and you have not updated all the copies of the database.
Other common reasons for facing anomalies are because of redundant or poorly constructed databases. At most times, performing normalisation can remove anomalies in DBMS. The process of normalisation reduces the likelihood of generating tables which may cause anomalies.
Conclusion
Concurrency is one of the most important aspects or properties of a database system. It must allow systems to run different transactions at the same time.
However, since resources are limited in a computer system, conflicts can occur. To reduce such chances of conflicts, a two-phase locking protocol is used.
The 2 phase locking protocol in DBMS allows transactions to lock the resources when required and then release them in the next phase when the transaction is completed.
The protocol works in two phases including the shrinking and growing phase and each process is required to lock and unlock resources in these phases only.