Table of contents
1.
Introduction
2.
What is Lock-Based Protocol in DBMS?
3.
Types of Locks in DBMS
3.1.
Shared Lock 
3.2.
Exclusive Lock
4.
Lock Compatibility Matrix
5.
Types of Locking protocol in DBMS 
5.1.
1. Simplistic Lock Protocol
5.2.
2. Pre-claiming Lock Protocol
5.3.
3. Two-Phase Locking Protocol
5.4.
4. Strict Two-Phase Locking Protocol
5.5.
5. Strong strict 2PL/ Rigorous 2PL
6.
Upgrade / Downgrade Locks
7.
Problems of Locking protocol in DBMS 
8.
Frequently Asked Questions
8.1.
What is the lock method in the database?
8.2.
How many locks are there in DBMS?
8.3.
What are the different types of locking methods?
8.4.
What is the two-phase locking protocol in RDBMS?
9.
Conclusion
Last Updated: Apr 30, 2024
Medium

Lock Based Protocol in DBMS

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A lock-based protocol in a Database Management System (DBMS) acts as a safeguard, ensuring that a transaction can't access or modify data until it acquires the required lock.

locking protocol in dbms

Locking protocol in DBMS synchronizes concurrent transactions' access to database elements. The concurrency-control manager receives all lock requests.

What is Lock-Based Protocol in DBMS?

Locking protocol in DBMS is like a set of rules for managing access to data in databases. With lock-based protocols, a transaction cannot read or write data until the appropriate lock is obtained. This helps to solve the concurrency problem by locking a given transaction to a specific user. The lock is a variable that indicates which operations can be carried out on the specific data item.

Also, read about - Implementation of Locking

Types of Locks in DBMS

There are two types of Locking protocol in DBMS : 

  • Shared Lock (S) 
  • Exclusive Lock (X)

Let's discuss these two types of locks in detail:

Shared Lock 

A shared lock is also known as a Read-only lock. The data item can be shared between transactions via the shared lock. We will not be able to update data on the lock.

Example of shared lock: Consider the situation where two transactions read a person's account balance. By establishing a shared lock on the database, they will read it. However, a shared lock prevents another transaction from updating that account's balance until the reading procedure is completed.

Exclusive Lock

The data item can be read and written via Exclusive Lock. This exclusive mode cannot be used on the same data item simultaneously. The lock-x instruction is used to request X-lock. Transactions may unlock the data item after completing the 'write' process.

Example of exclusive lock: When a transaction needs to update a person's account balance, for example, you can allow it to proceed by putting an X lock on this transaction. As a result, the exclusive lock prevents the second transaction from reading or writing.

Shared Lock 

Exclusive Lock 

  • A transaction T having been shared can only read the data. 
  • More than one transaction can acquire a shared lock on the same variable.
  • Represented by S  
  • A transaction T having an exclusive lock can both read as well as write 
  • At any given time, only one Transaction can have an exclusive lock on a variable.
  • Represented by X

Lock Compatibility Matrix

The lock compatibility matrix outlines which locks can be acquired together without causing deadlock or contention. It defines rules for the combination of locks that can be held concurrently by multiple threads or processes to ensure safe and efficient synchronization in a multi-threaded or multi-process environment.

Types of Locking protocol in DBMS 

  • Simplistic lock protocol
     
  • Pre-claiming Lock Protocol
     
  • Two-phase locking (2PL)
     
  • Strict Two-Phase Locking Protocol
     
  • Strong strict 2PL/ Rigorous 2PL

1. Simplistic Lock Protocol

Simplistic Locking protocol let transactions get a lock on each object before performing a "write" operation. After successfully executing the "write" procedure, transactions may unlock the data item.

2. Pre-claiming Lock Protocol

Pre-claiming Lock Protocols is well known to analyze transactions to decide which data items call for locks. It requests all of the locks on all of the data objects from the database management system before actually beginning the transaction. If all of the locks are obtained, the pre-claiming protocol lets the transaction to start. The lock is opened once the transaction has been completed. This procedure waits until all of the locks are granted before allowing the transaction to roll back if some of the locks are not granted.

3. Two-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. The Two-Phase Locking strategy aids in the elimination of the DBMS concurrency problem. It guarantees conflict serializability in turn guarantees serializability.

Conflict serializability => Serializability 

This locking protocol has 2 phases. 

Phase 1:- Growing phase / expanding phase: acquires Lock.

Phase 2:- Shrinking phase: release locks 

Rules are given in the form of 2 phases where rules are as follows : 

  1.  Ti cannot acquire a lock after a lock has been released
     
  2.  On commit/ rollback, all locks are released by default
     
Two-Phase Locking Protocol

Read more about - Two Phase Locking Protocol

4. Strict Two-Phase Locking Protocol

Strict-Two phase locking is nearly identical to 2PL. Strict-2PL differs in that it never releases a lock after using it. It keeps all the locks until the commit point. It then releases them all simultaneously when the procedure is finished—strict 2PL guarantees both serializability and recoverability (strict recoverable).

It follows all rules of 2PL, plus all exclusive locks must be held till commit/rollback, but there is no such restriction with shared Lock.

Strict Two-Phase Locking Protocol

Strict 2PL=>CSS+Strict recoverable 

5. Strong strict 2PL/ Rigorous 2PL

Rigorous 2-PL necessitates the release of any Exclusive(X) and Shared(S) locks held by the transaction until after the Transaction Commits, in addition to the Lock being 2-Phase. Our schedule will be Recoverable and Cascadeless if we follow Rigorous 2-PL.

As a result, it frees us from Cascading Abort, which was still present in Basic 2-PL, and also guarantees Strict Schedules, though Deadlocks are still possible!

2PL + all locks (shared or exclusive) should be held till transaction commit or rollback.

Strong strict 2PL

In the above example, S2 has an exclusive lock and is released after committing because S2 is a strict 2PL. But it is not rigorous 2PL.

You can also read about the Log-based recovery.

Upgrade / Downgrade Locks

Upgrading locks means converting the multiple locks that have the same type into a higher-level lock. It is done to get more efficiency. Downgrading locks means converting the high-level locks to the low-level locks. It helps to allow other transactions to access the data. This helps to manage locks efficiently. Also, it ensures the smooth data access in a multi-user database environment.

Also read- multiple granularity in dbms

Problems of Locking protocol in DBMS 

  • Deadlocks happen when several transactions are awaiting the release of locked data objects, resulting in a deadlock situation. This can cause the entire system to stop
     
  • Blocking can result from lock-based protocols when a transaction is made to wait forever for another transaction to release its locks. The responsiveness and overall performance of system may suffer as a result
     
  • Lower-priority transactions may encounter delays or never finish in circumstances where higher-priority operations routinely obtain locks they require, creating a state known as starvation
     
  • The amount of concurrency in the database system is constrained by lock-based protocols. This can make it difficult for system to manage several transactions at once because only one transaction can have exclusive access to a data item at any given moment

Frequently Asked Questions

What is the lock method in the database?

The lock method in a database refers to the use of locks to control access to data. Locks can be employed to prevent multiple transactions from concurrently modifying the same data, ensuring consistency and preventing conflicts in a multi-user database environment.

How many locks are there in DBMS?

In DBMS, there are two main types of locks: shared locks and exclusive locks. Shared locks allow multiple transactions to read a resource simultaneously, while exclusive locks permit only one transaction to modify the resource, preventing others from accessing it concurrently.

What are the different types of locking methods?

Common locking methods include:

  • Exclusive Locks: Allows only one thread/process to access the resource.
  • Shared Locks: Allows multiple threads/processes to read the resource simultaneously.

What is the two-phase locking protocol in RDBMS?

The two-phase locking protocol in RDBMS is a concurrency control method. It consists of two phases: the growing phase, during which transactions acquire locks, and the shrinking phase, where no new locks are acquired. This ensures that once a transaction releases a lock, it cannot acquire any new locks, preventing potential deadlocks and ensuring consistency.

Conclusion

In this blog, we have learned that A lock is a data variable that is linked to a specific data item. This lock indicates that activities on the data item are permitted. With the Exclusive Lock, a data item can be read as well as written but with the Shared Lock item can be read-only. The Two-Phase Locking Protocol aids in the elimination of the concurrency problem. It guarantees conflict serializability in turn, guarantees serializability. Strict 2PL follows all rules of 2PL + all exclusive locks must be held till commit/rollback but there is no such restriction with shared lock. Rigorous 2-PL necessitates the release of any Exclusive(X) and Shared(S) locks held by the transaction until after the Transaction Commits, in addition to the lock being 2-Phase.

Check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top tech companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

You can also consider our DBMS Course to give your career an edge over others.

Live masterclass