Two-Phase Locking Protocol

Shivani Kumari
Last Updated: May 13, 2022

Introduction

In lock-based protocol, a transaction cannot access or write data unless it obtains a suitable lock. There are two sorts of locks: 

  1. Shared lock: The transactions can only read the data items in a shared lock.
  2. Exclusive lock: The transactions can read and write to the data items in an exclusive lock.


There are four types of lock protocols:

  1. Simplistic lock protocol
  2. Pre-claiming lock Protocol
  3. Two-phase locking (2PL)
  4. Strict Two-phase locking (Strict-2PL)


In this article, we will only discuss the two-phase locking protocol.

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.


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.

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. Click here to know more about deadlock.

Frequently Asked Questions

  1. What is 2 phase locking protocol?
    The two-phase Locking Protocol, often known as the 2PL protocol, is a method of concurrency control in DBMS that maintains serializability by securing transaction data with a lock that prevents subsequent transactions from accessing the same data at the same time. 
     
  2. Can two-phase locking generate deadlock?
    Yes, there is a possibility of deadlocks in two-phase locking. A deadlock occurs in a two-phase locking when a transaction is waiting for an item, locked by another transaction.
     
  3. 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, there is no guarantee of serializability.
     
  4. Does a two-phase locking protocol ensure conflict serializability?
    Serializability is guaranteed by the 2PL protocol. The transactions can be serialized in the order of their lock points.

Key Takeaways

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.

Ready to solve problems asked in the interviews? 

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

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 CodeStudio 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.

Was this article helpful ?
1 upvote

Comments

No comments yet

Be the first to share what you think