The Wayback Machine - https://web.archive.org/web/20241114082648/https://www.geeksforgeeks.org/two-phase-locking-protocol/
Open In App

Two Phase Locking Protocol

Last Updated : 11 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Pre-Requisite: Concurrency Control Protocol, Lock-based Protocol. 

Now, recalling where we last left off, there are two types of Locks available Shared S(a) and Exclusive X(a). Implementing this lock system without any restrictions gives us the Simple Lock-based protocol (or Binary Locking), but it has its own disadvantages, they do not guarantee Serializability. Schedules may follow the preceding rules but a non-serializable schedule may result.

To guarantee serializability, we must follow some additional protocols concerning the positioning of locking and unlocking operations in every transaction. This is where the concept of Two-Phase Locking(2-PL) comes into the picture, 2-PL ensures serializability. Now, let’s dig deep!

Two Phase Locking

A transaction is said to follow the Two-Phase Locking protocol if Locking and Unlocking can be done in two phases.

  • Growing 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.

Note: If lock conversion is allowed, then upgrading of lock( from S(a) to X(a) ) is allowed in the Growing Phase, and downgrading of lock (from X(a) to S(a)) must be done in the shrinking phase.

Let’s see a transaction implementing 2-PL.

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

This is just a skeleton transaction that shows how unlocking and locking work with 2-PL. Note for:

Transaction T1

  • The growing Phase is from steps 1-3
  • The shrinking Phase is from steps 5-7
  • Lock Point at 3

Transaction T2

  • The growing Phase is from steps 2-6
  • The shrinking Phase is from steps 8-9
  • Lock Point at 6

Lock Point

The Point at which the growing phase ends, i.e., when a transaction takes the final lock it needs to carry on its work. Now look at the schedule, you’ll surely understand. I have said that 2-PL ensures serializability, but there are still some drawbacks of 2-PL. Let’s glance at the drawbacks.

Cascading Rollbacks in 2-PL

Let’s see the following Schedule

Cascading RollBack

Cascading Roll-Back

Take a moment to analyze the schedule. Yes, you’re correct, because of Dirty Read in T2 and T3 in lines 8 and 12 respectively, when T1 failed we have to roll back others also. Hence, Cascading Rollbacks are possible in 2-PL. I have taken skeleton schedules as examples because it’s easy to understand when it’s kept simple. When explained with real-time transaction problems with many variables, it becomes very complex.

Deadlock in 2-PL

Consider this simple example, it will be easy to understand. Say we have two transactions T1 and T2. 

Schedule:   Lock-X1(A)   Lock-X2(B)  Lock-X1(B)  Lock-X2(A)

Drawing the precedence graph, you may detect the loop. So Deadlock is also possible in 2-PL.

Two-phase locking may also limit the amount of concurrency that occurs in a schedule because a Transaction may not be able to release an item after it has used it. This may be because of the protocols and other restrictions we may put on the schedule to ensure serializability, deadlock freedom, and other factors. This is the price we have to pay to ensure serializability and other factors, hence it can be considered as a bargain between concurrency and maintaining the ACID properties.

The above-mentioned type of 2-PL is called Basic 2PL. To sum it up it ensures Conflict Serializability but does not prevent Cascading Rollback and Deadlock. Further, we will study three other types of 2PL, Strict 2PL, Conservative 2PL, and Rigorous 2PL.

Problem with Two-Phase Locking

  • It does not insure recoverability which can be solved by strict two-phase locking and rigorous two-phase locking.
  • It does not ensure a cascade-less schedule which can be solved by strict two-phase locking and rigorous two-phase locking.
  • It may suffer from deadlock which can be solved by conservative two-phase locking.

GATE-Related Questions

  1. GATE CS 2016-2 | Question 61
  2. GATE CS 1999 | Question 31

Frequently Asked Questions

1. What is 2-Phase Locking Protocol?

Answer:

2-Phase Locking Protocol is a concurrency protocol that helps in serializability.

2. What are the types of the 2-Phase Locking Protocol?

Answer:

There are basically three types of 2-Phase Locking Protocol:

  • Strict 2PL
  • Conservative 2PL
  • Rigorous 2PL


Previous Article
Next Article

Similar Reads

Two Phase Locking (2-PL) Concurrency Control Protocol | Set 3
Prerequisite - Basics of Two-Phase Locking protocol(2-PL), Types of 2-PL. Now, we know both Strict 2-PL and Rigorous 2-PL avoids Cascading Rollbacks and ensures a Strict schedule but still cannot guarantee that our schedule is Deadlock free. We have seen both Strict and Rigorous 2-PL are similar in application and a general misconception is common
3 min read
Categories of Two Phase Locking (Strict, Rigorous & Conservative)
Now that we are familiar with what is Two-Phase Locking (2-PL) and the basic rules which should be followed which ensures serializability. Moreover, we came across problems with 2-PL, Cascading Aborts, and Deadlocks. Now, we turn towards the enhancements made on 2-PL which try to make the protocol nearly error-free. Briefly, we allow some modificat
4 min read
MOSS Concurrency Control Protocol (Distributed Locking in Database)
This is a protocol which is used to control the concurrency in the Distributed database environment, Here we'll read about the rules and regulations that are required to keep in mind while applying MOSS Concurrency Control Protocol. MOSS Concurrency Control Protocol:- a) It is mainly used for handling the nested(hierarchical) transactions which are
2 min read
Two Phase Commit Protocol (Distributed Transaction Management)
Consider we are given with a set of grocery stores where the head of all store wants to query about the available sanitizers inventory at all stores in order to move inventory store to store to make balance over the quantity of sanitizers inventory at all stores. The task is performed by a single transaction T that's component Tn at the nth store a
5 min read
Recovery from failures in Two Phase Commit Protocol (Distributed Transaction)
Prerequisite: Two-Phase Commit ProtocolIn the 2-phase commit protocol, the sites contributing to a distributed transaction and the coordinator that is managing the whole transaction globally may fail or crash, and this could lead to the whole transaction failure. Since unanimity is required in order to commit a distributed transaction successfully
4 min read
Three Phase Commit Protocol
Three-Phase Commit (3PC) Protocol is an extension of the Two-Phase Commit (2PC) Protocol that avoids blocking problem under certain assumptions. In particular, it is assumed that no network partition occurs, and not more than k sites fail, where we assume 'k' is predetermined number. With the mentioned assumptions, protocol avoids blocking by intro
3 min read
Difference between Stop and Wait protocol and Sliding Window protocol
Both the Stop and Wait protocol and the Sliding Window protocol are the techniques to the solution of flow control handling. The main difference between the Stop-and-wait protocol and the Sliding window protocol is that in the Stop-and-Wait Protocol, the sender sends one frame and waits for acknowledgment from the receiver whereas in the sliding wi
5 min read
Implementation of Locking in DBMS
Locking protocols are used in database management systems as a means of concurrency control. Multiple transactions may request a lock on a data item simultaneously. Hence, we require a mechanism to manage the locking requests made by transactions. Such a mechanism is called a Lock Manager. It relies on the process of message passing where transacti
5 min read
Levels of Locking in DBMS
The locking in a database can be done at 4 levels, which start with the database at the highest level and down via table and page to the row at the lowest level. Database LevelTable LevelPage-LevelRow Level Before we discuss the levels of locking, we should know about the types of locks or lock mode. So, there are 5 locks types in the locking and t
6 min read
Predicate Locking
A lock, in DBMS, is a variable that is associated with a data item. Locks in DBMS help synchronize access to the database items by concurrent transactions. Lock Based Protocol in DBMS is used to eliminate concurrency problems for simultaneous transactions, (this refers to a situation wherein one transaction results in changing the results of anothe
3 min read
Multiple Granularity Locking in DBMS
The various Concurrency Control schemes have used different methods and every individual Data item is the unit on which synchronization is performed. A certain drawback of this technique is if a transaction Ti needs to access the entire database, and a locking protocol is used, then Ti must lock each item in the database. It is less efficient, it w
5 min read
Phase Modulation
Phase modulation (PM) is one of the modulation techniques that involves the use of a phase angle to modulate a carrier signal. While in AM, the carrier signal is varied in terms of its strength, the phase angle of the carrier signal is varied in PM depending on the input signal. This modulation technique is essential in most digital communication s
7 min read
Graph Based Concurrency Control Protocol in DBMS
Graph-based concurrency control protocols are used in database management systems (DBMS) to ensure that concurrent transactions do not interfere with each other and maintain the consistency of the database. In graph-based concurrency control, transactions are represented as nodes in a graph, and the conflicts between transactions are represented as
4 min read
Features of Enhanced Interior Gateway Routing Protocol (EIGRP)
Enhanced Interior Gateway Routing Protocol (EIGRP) is a Cisco-proprietary Hybrid routing protocol that contains features of distance-vector and link-state routing protocols. It is a network layer protocol that works on protocol number 88. Some of its features are: Rapid convergence - EIGRP uses a DUAL algorithm to support rapid convergence. If a ro
4 min read
Open Shortest Path First (OSPF) Protocol fundamentals
Open shortest path first (OSPF) is a link-state routing protocol that is used to find the best path between the source and the destination router using its own shortest path first (SPF) algorithm. A link-state routing protocol is a protocol that uses the concept of triggered updates, i.e., if there is a change observed in the learned routing table
5 min read
Types of Spanning Tree Protocol (STP)
Prerequisite - Spanning Tree Protocol Spanning Tree Protocol (STP) is used to make a loop free network by monitoring the network to track all the links and shut down the least redundant ones. Root bridge is a switch in a single VLAN or whole topology (according to the type of STP standard used) which is responsible for distributing BPDUs and block
3 min read
IGRP Routing Protocol
In a host network, the Interior Gateway Routing Protocol (IGRP) is a proprietary distance vector routing protocol that is used to exchange routing information. Cisco was the one who came up with the idea. The Interior Gateway Routing Protocol (IGRP) regulates the transfer of routing information among linked routers in the host network or autonomous
5 min read
Efficiency of Stop and Wait Protocol
Stop and Wait is a flow control protocol. In which the sender sends one packet and waits for the receiver to acknowledge and then it will send the next packet. In case if the acknowledgement is not received, the sender will retransmit the packet. This is the simplest one and easy to implement. but the main disadvantage is the efficiency is very low
3 min read
Validation Based Protocol in DBMS
Validation Based Protocol is also called Optimistic Concurrency Control Technique. This protocol is used in DBMS (Database Management System) for avoiding concurrency in transactions. It is called optimistic because of the assumption it makes, i.e. very less interference occurs, therefore, there is no need for checking while the transaction is exec
5 min read
Gossip Protocol in Cassandra
In this article, we'll be looking briefly at the Gossip protocol that is implemented in the Apache Cassandra database. In this article, we will discuss Gossip protocol and How Cassandra achieves coordination between nodes and How nodes stay in-sync. In Cassandra, all nodes are the same and have peer to peer architecture and there is no concept of a
3 min read
Stop and Wait protocol, its problems and solutions
It is the simplest flow control method in which the sender will send the packet and then wait for the acknowledgement by the receiver that it has received the packet then it will send the next packet. Stop and wait protocol is very easy to implement. Total time taken to send is, Ttotal = Tt(data) + Tp + Tq + Tprocess + Tt(ack) + Tp ( since, Tq and
2 min read
Real-time Transport Control Protocol (RTCP)
Real-time Transport Protocol (RTP) allows only that type of message, which carries data from the source to the destination. But in some cases, we need some other type of messages in a session. The messages that can control the transmission and quality of data as well as also allow the recipients so that they can send feedback to the source or sourc
3 min read
Working of GOSSIP protocol in Cassandra
In this article, we will discuss the overview of Gossip protocol and mainly focus on working of gossip protocol and what is the role in the cluster to communicate with other nodes. Let's discuss it one by one. Gossip protocol :In Cassandra, the nodes communicate with each other that is through gossip protocol. The main role of the gossip protocol i
2 min read
Noiseless Channel Protocol
Introduction :A protocol is a set of rules used by two devices to communicate. These sets of rules are usually decided by headers (fixed headers determined by the protocol). These headers specify the content of the message and the way this message is processed. To detect the error, the header must be the address of the destination, the address of t
4 min read
Derive the efficiency of Pure ALOHA protocol
The efficiency of an ALOHA system is that fraction of all transmitted frames which escape collisions that is which do not get caught in collisions.Consider ∞ number of interactive users at their computers (stations). Each user is either typing or waiting. Initially, all of them are in the typing state. When a user types a line, the user stops and w
3 min read
Types of ICMP (Internet Control Message Protocol) Messages
Pre-Requisite: Internet Control Message Protocol (ICMP) Internet Control Message Protocol is a protocol used for error handling and debugging in the network layer. It is mainly used in network devices such as routers for sending errors and operations. The main work of the Internet Control Message Protocol is of reporting errors, between any two dev
4 min read
When Transmission Control Protocol (TCP) most appropriate?
TCP(Transmission Control Protocol) is a protocol of the Transport layer. TCP is a connection-oriented, reliable, and secured protocol. To provide reliable service TCP implements Error control, Congestion control, Flow control. Services provide by TCP : The process to process communication -It is achieved using the port number.Stream Delivery Servic
2 min read
Protocol Data Unit (PDU)
The OSI model, which was first introduced by the International Organization for Standardization (ISO) in 1977, is a reference that specifies the transferring of data from one computer to another computer. The layered stack of the OSI (Open System Interconnection) reference model is made up of seven layers and each has a specific communication purpo
4 min read
Sliding Window Protocol | Set 3 (Selective Repeat)
Prerequisite : Sliding Window Protocol - Set 1 (Sender Side), Set 2 (Receiver Side) Why Selective Repeat Protocol? The go-back-n protocol works well if errors are less, but if the line is poor it wastes a lot of bandwidth on retransmitted frames. An alternative strategy, the selective repeat protocol, is to allow the receiver to accept and buffer t
3 min read
Commit Protocol in DBMS
This article covers topics related to the database management system. In this article, we will learn about the commit protocols that are in the subject of database management systems. This article then describes the types of Commit protocols in database management systems. It then talks about the advantages of a Database Management System. Commit P
5 min read
three90RightbarBannerImg