Table of contents
1.
Introduction
1.1.
Types of Locks Used in Transaction Control 
2.
What is Two-phase locking (2PL)?
2.1.
Lock Point
2.2.
Transaction T1:
2.3.
Transaction T2:
3.
Types of Two-Phase Locking Protocol
3.1.
1. Strict two-phase locking protocol
3.2.
2. Rigorous two-phase locking protocol
3.3.
3. Conservative two-phase locking protocol
4.
Deadlock in a two-phase locking
5.
Cascading Rollbacks in 2-PL
6.
Problem with Two-Phase Locking
7.
Frequently Asked Questions
7.1.
What are the advantages of two phase locking protocol?
7.2.
What are the two disadvantages or problems of lock-based protocols?
7.3.
What is the purpose of Two-Phase Locking?
7.4.
Why do we use two-phase locking protocol?
7.5.
What is the difference between Two-Phase Locking and two-phase commit protocol?
8.
Conclusion
9.
Ready to solve problems asked in the interviews? 
Last Updated: May 4, 2024
Medium

Two Phase Locking Protocol

Author Shivani Kumari
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In lock-based protocol, a transaction cannot access or write data unless it obtains a suitable lock. The Two-Phase Locking Protocol ensures transaction consistency by employing a set of rules. Transactions must obtain appropriate locks before accessing or modifying data, preventing conflicts and maintaining data integrity throughout the transaction's lifecycle.

two phase locking protocol in dbms

Types of Locks Used in Transaction Control 

In transaction control, there are two common types of locks shared lock and exclusive lock. Here is a brief overview about it:

  1. Shared lock: The transactions can only read the data items in a shared lock.
  • While a transaction attempting to update the data will be unable to do so until the shared lock is freed, a transaction attempting to read the same data will be allowed to do so.
  • Read lock is another name for shared lock, which is exclusively used for reading data objects.


 2. Exclusive lock: The transactions can read and write to the data items in an exclusive lock.

  • When a statement updates data, its transaction has an exclusive lock on the modified data, preventing access by other transactions.
  • Until the transaction holding the lock commits or rolls it back, the lock is in effect.

Also read- multiple granularity in dbms

What is Two-phase locking (2PL)?

The two-phase locking divides the transaction execution phase into three components.

  • When the transaction's execution begins in the first part, it requests permission for the lock it requires.
  • The transaction then obtains all of the locks in the second part. The third phase begins when the transaction's first lock is released.
  • The transaction cannot demand any new locks in the third phase. It only unlocks the locks that have been acquired.


A transaction follows the two-phase locking protocol if locking and unlocking can be done in two phases. 

The two phases of the two-phase protocol are:

  • Growing phase: During the growth phase, new locks on data items may be acquired, but none can be released.
  • Shrinking phase: Existing locks may be released, but no new locks can be acquired during the shrinking phase.


Note: If lock conversion is allowed, the following phase may take place:

  1. Upgrading of lock (from S(a) → X(a)) is allowed in the growing phase.
  2. Downgrading of lock (from X(a) → S(a)) must be done in the shrinking phase.
Two-phase locking (2PL)

The depicted diagram illustrates the growing phase, where locks are acquired until all necessary transaction locks are obtained, defining the Lock-Point. Beyond this point, transactions transition into the Shrinking phase, ensuring a structured lock-based protocol.


Lock Point

It is a point at which the growing phase comes to a close,  i.e., when a transaction obtains the last lock it requires.

Let’s look at an example:

 

T1

T2

1

Lock - S(A)

 

2

 

Lock - S(A)

3

Lock - X(B)

 

4

…….

…….

5

Unlock(A)

 

6

 

Lock - X(C)

7

Unlock(B)

 

8

 

Unlock(A)

9

 

Unlock(C)

10

…….

…….

The way unlocking and locking works in two-phase locking is:

Transaction T1:

  • The growing phase is from steps 1-3.
  • The shrinking phase is from steps 5-7.
  • The locking point is at point 3.

Transaction T2:

  • The growing phase is from steps 2-6.
  • The shrinking phase is from steps 8-9.
  • The locking point is at point 6.


The type mentioned above of 2-PL is Basic 2PL. It ensures Serializability, but it does not prevent Cascading Rollback and Deadlock.

Types of Two-Phase Locking Protocol

Two Phase Locking is classified into three types :

1. Strict two-phase locking protocol

  • Exclusive(X) locks held by the transaction be released after committing in addition to the lock being 2-Phase.
  • Ensures that the schedule is recoverable and cascadeless.

2. Rigorous two-phase locking protocol

  • Exclusive(X) and Shared(S) locks held by the transaction be released after Transaction Commits, in addition to the lock being 2-Phase.
  • It ensures that schedule is recoverable and cascadeless.

3. Conservative two-phase locking protocol

  • Static 2-PL enforces pre-locking of all accessed resources by declaring read-set and write-set before transaction execution.
  • Transaction waits if necessary items can't be locked, avoiding locking any items if a predeclared requirement isn't met.

Deadlock in a two-phase locking

Deadlocks are possible in a 2PL. You can detect the deadlocks by drawing the precedence graph of the transactions schedule, and if you find a loop, then there is a deadlock situation.

Deadlocks are needed to be resolved to complete the transaction. A deadlock is resolved by aborting a transaction of the given loop and breaking it. 

If you want to know more about Deadlock, please check this out - Deadlocks In OS.

Let's understand deadlock in a two-phase locking protocol with the help of an example.

Suppose there are two transactions, T1 and T2. If the completion of transaction T1 is dependent on the completion of transaction T2 and the completion of transaction, T2 is dependent on the completion of transaction T1. This results in a state of deadlock. As shown in the figure below:

Deadlock in a two-phase locking

Phase 1 - Lock Request:

Process T1 acquires a lock on Resource A. 

Process T2 acquires a lock on Resource B.

Phase 2 - Lock Release:

Process T1 now needs Resource B to proceed further, but it cannot release Resource A until it has both resources due to the two-phase locking rule.
Process T2 similarly needs Resource A to continue, but it's holding onto Resource B.

There are three more types of lock protocols:

  • Simplistic lock protocol
  • Pre-claiming lock Protocol
  • Strict Two-phase locking (Strict-2PL)

Cascading Rollbacks in 2-PL

In Two-Phase Locking (2-PL), transactions acquire locks in two phases: the growing phase, where locks are acquired, and the shrinking phase, where locks are released. Cascading rollbacks occur when a transaction needs to be rolled back due to a conflict with another transaction, causing all subsequent transactions dependent on it to also be rolled back. This cascading effect can lead to a loss of consistency and inefficiency in the database system.

Problem with Two-Phase Locking

Two-phase locking (2-PL) ensures serializability by acquiring and releasing locks in two phases. However, it suffers from several drawbacks:

  1. Deadlocks: 2-PL can lead to deadlocks, where transactions wait indefinitely for locks held by other transactions.
  2. Cascading Rollbacks: As mentioned earlier, cascading rollbacks can occur in 2-PL, leading to inefficiency and inconsistency.
  3. Lock contention: Excessive lock contention can degrade performance, especially in high-concurrency environments.
  4. Strictness: 2-PL's strictness can limit concurrency, leading to decreased throughput and increased response times.

Frequently Asked Questions

What are the advantages of two phase locking protocol?

Some of the advantages of using a two-phase locking protocol are serializability, deadlock prevention, compatibility with many other applications, simplicity, isolation, tuning with lock timeout mechanisms, and concurrency control.

What are the two disadvantages or problems of lock-based protocols?

Simple locking's drawbacks include: Multiple transactions have inconsistencies in their data, Deadlock situations can occur, and there is no guarantee of serializability.

What is the purpose of Two-Phase Locking?

Mainly, the Two-Phase Locking protocol ensures isolation, consistency, and correctness of the transactions in a database management system. It helps when multiple transactions occur at the same time. It achieves it by acquiring and releasing locks on the data sets.

Why do we use two-phase locking protocol?

We must adhere to an additional protocol for the placement of locking and unlocking operations in each transaction in order to ensure serializability. The idea of Two-Phase Locking (2-PL), which guarantees serializability, enters the picture at this point. Therefore, two phase locking protocols are required in these kinds of situations.

What is the difference between Two-Phase Locking and two-phase commit protocol?

Two-Phase Locking is a method for managing database concurrency through lock acquisition and release stages. On the other hand, the Two-Phase Commit Protocol guarantees distributed transaction integrity using coordinator-led prepare and commit phases across multiple nodes.

Conclusion

This article taught about lock-based protocols, especially two-phase locking protocols.

We learned that there are four types of lock protocols:

  • Simplistic lock protocol
  • Pre-claiming Lock Protocol
  • Two-phase locking (2PL)
  • Strict Two-phase locking (Strict-2PL)


Focusing on the 2PL protocol, we learned that there are two phases of the 2PL, which includes:

  1. Growing phase
  2. Shrinking phase.


Then we came across the drawbacks of the 2PL, i.e., Cascading Rollback and Deadlock.

Happy Coding!

Ready to solve problems asked in the interviews? 

Enroll today and Practice problems asked in technical interview rounds and learn some tricks to solve them faster and more confidently.

Check out this article - File System Vs DBMS

Also read - multiple granularity in dbms

Also, you can check out the Top 100 SQL Problems to get hands-on experience with frequently asked interview questions and land your dream job.

Apart from that, you can use Coding Ninjas Studio to practice a wide range of DSA questions asked in lots of interviews. It will assist you in mastering efficient coding techniques, and you will also get interview experiences from people working in big companies.

Live masterclass