View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
  1. Home
DBMS

DBMS Tutorial: Learn Database Management Basics

Learn the fundamentals of Database Management Systems (DBMS) with our comprehensive tutorial. Understand key concepts and practical applications. Start now!

  • 32
  • 8 Hours
right-top-arrow
20

Lock Based Protocol in DBMS

Updated on 30/07/2024372 Views

In the wide landscape of database management systems (DBMS), data integrity and concurrency control are the key things to be ensured. The lock-based protocol in DBMS is a method for regulating synchronized access to shared resources in multi-user systems, including database records. Lock-based protocols control data corruption and ensure consistency by controlling the access of multiple transactions to the same data file.

Lock-based protocols in DBMS are based on locking, where a transaction must first obtain a lock on a resource before it can access the resource. This excludes other processes that can update the data at the same time to ensure the integrity of data. Locks in DBMS can operate at different levels such as databases, tables, rows, and even specific data items.

Types of Lock-Based Protocols in DBMS

Lock-based protocols in DBMS play an important role in managing parallel requests to shared system resources. These protocols enforce data integrity and consistency by providing access control between conflicting and concurrent transactions to the same data simultaneously. DBMS uses several sorts of lock-based protocols, each with traits suitable for different environments. 

Two-Phase Locking (2PL)

Two-phase locking is one of the most popular lock-based protocols utilized in a DBMS.

Mechanism: Transactions occur in two successive phases, growing and shrinking. During the growing phase, transactions acquire locks on all the data items they may need before executing any operations. After a transaction releases a lock, it stops acquiring any new locks. In the shrinking phase, transactions keep all their locks. 

Two-phase locking allows transaction serializability by making transactions acquire locks following a specific order and keep them until the transaction completes. Encompasses Strict Two-Phase Locking (Strict 2PL), where all the transactions lock the data until they commit or abort. 

Here is an example of two-phase locking implemented using pseudocode:

// Transaction T1

begin_transaction(T1);

lock(X, exclusive); // Acquire exclusive lock on data item X

read(X);

X = X + 10;

write(X);

unlock(X); // Release lock on data item X

commit(T1); // Transaction T1 commits

// Transaction T2

begin_transaction(T2);

lock(X, shared); // Acquire shared lock on data item X

read(X);

print(X);

unlock(X); // Release lock on data item X

commit(T2); // Transaction T2 commits

This example is of Transaction T1, which receives an exclusive lock on data item X, performs read and write operations on X, and then releases the lock before committing. In transaction T2, X gets shared lock access followed by a read on its value, and then it is released before the transaction is committed. The locks guarantee that T1 and T2 can be executed concurrently only with one of them having exclusive access to X.

Timestamp-Based Protocols

This protocol uses timestamps to keep transactions ordered and executed safely.

Mechanism: Every transaction starts with a special timestamp. Transactions are processed based on their timestamps, and conflicts are settled according to timestamp order. Timestamp-based protocols preserve conflict serializability through the mechanism that makes immutable the access of data by transactions in case of timestamp ordering violation. 

Deadlock Detection and Prevention

Deadlock detection and prevention protocols are another lock-based protocol in DBMS that aims to spot a deadlock and prevent transactions from waiting indefinitely for claimable resources. Approaches such as wait-die and wound-wait are used to indemnify against a deadlock. In the deadlock of the wait-die type, the younger transactions wait for older ones, while in the deadlock of the wound-wait type, the older ones abort the younger ones to handle conflict.

Here is an example of deadlock prevention implemented using pseudocode:

// Transaction T1

begin_transaction(T1);

lock(X, exclusive); // Acquire exclusive lock on data item X

lock(Y, exclusive); // Acquire exclusive lock on data item Y

// Perform operations on X and Y

unlock(Y); // Release lock on data item Y

unlock(X); // Release lock on data item X

commit(T1); // Transaction T1 commits

// Transaction T2

begin_transaction(T2);

wait_until(T1.commits); // Wait until T1 commits

lock(Y, exclusive); // Acquire exclusive lock on data item Y

// Perform operations on Y

unlock(Y); // Release lock on data item Y

commit(T2); // Transaction T2 commits

In this instance, process T1 acquires locks entitled to data X and Y. Transaction T2 locks the mutual lock of Y until T1 releases its lock. When T1 has finished, T2 is the only one waiting for locks, which prevents T1 and T2 from waiting for each other.

Granular Locking

In granular locking, the locking facilities are availed at different levels of granularity such as database level, table level, row level, or even data item level.

Mechanism: The problem has at least two possible solutions. The first solution is to lock resources that have been acquired during transactions. This reduces the number of lock contentions and improves concurrency. Fine-grained locking is good for concurrency but it can increase the overhead because of the increased complexity of lock management. Locking a grainy system reduces overhead at the cost of concurrency possibility.

Read and Write Locks

The read and write lock protocol is also a type of lock-based protocol in DBMS that permits having multiple or several users on the same resource. The shared locks (read locks) permit the reading of data concurrently by multiple transactions but also the prevention of writing operations. The locked resource (read locks) is accessible only for exclusive reads and writes, meaning that only one transaction can access it at a time.

Here is an example of read and write locks implemented using pseudocode:

// Transaction T1

begin_transaction(T1);

lock(X, exclusive); // Acquire exclusive lock on data item X

read(X);

X = X + 10;

write(X);

unlock(X); // Release lock on data item X

commit(T1); // Transaction T1 commits

// Transaction T2

begin_transaction(T2);

lock(X, shared); // Acquire shared lock on data item X

read(X);

print(X);

unlock(X); // Release lock on data item X

commit(T2); // Transaction T2 commits

In this instance, the statement T1 read/write acquires a shared (R/S) lock on data item X, thus able to perform both read and write operations. By applying this shared lock, T2 can read the value of X simultaneously with T1's operations but has to wait if T1 has the exclusive lock on X before writing to it.

Working of Lock-Based Protocol in DBMS

The working of lock-based protocols in DBMS involves many key components and a detailed process. Locks can be of different kinds, for example, shared locks and exclusive locks for granting the sort of access required by the transaction.

Lock Compatibility

Lock-based protocols in DBMS make transactions inform each other of their lock epochs to secure the compatibility and safety of data. Concurrent shared locks can be held by multiple transaction processes at the same time for reading; however, incompatible with exclusive locks (preventing write operations) would isolate the other processes. Exclusive locks can't be used with both shared and exclusive locks because the reading and writing operations of other transactions with the same data item will be prevented.

Concurrency Control

Concurrently enforced protocols utilize lock-based mechanisms for managing the locks that can be acquired or released depending on the need to guarantee serializability and eliminate data conflicts. Transactions implement certain rules to ensure consistency and the isolation of views on the data by adopting locks. In two-phase locking, transactions obtain all necessary locks before they start to work (the growing phase) and afterward release all locks at once (the shrinking phase).

Lock Release

When a transaction is finished and is no longer of any use for a data item, it releases the related lock to let other transactions work with it. Locks are released upon the commitment of the transaction (executing without failure) or aborted (rolled back due to failures or conflicts).  (executing without failure) or aborted (rolled back due to failures or conflicts). Rapid release of locks is a crucial factor for the system not to suffer contention and at the same time provide concurrency in the system.

Lock-Based Protocols in DBMS Example

Best practices for lock-based protocols in DBMS examples are given below:

  • Choose Appropriate Isolation Levels: While working on lock-based protocols in DBMS implement the transaction isolation levels that suit the requirements of the application. Higher isolation levels give us stronger guarantees but can hamper parallel processes.
  • Use Granular Locking: Implement fine-grained locking techniques to allow for concurrency and to prevent contention. Pick the right level of granularities for the locks (row-level, table-level) according to the access patterns as well as the contention amongst the application.
  • Avoid Long-Held Locks: Reduce the duration for which the locks are held as little as possible to reduce contention and increase concurrent operations. Use undertaking transactions to get locking if it is critically needed and release it immediately when it is no longer required.

Real-Life Examples of Lock-Based Protocols in DBMS

Real-life examples of lock-based protocols in DBMS are given below:

  • E-Commerce Transactions: On an e-commerce platform, several people can try to buy the same product simultaneously. The system is designed to lock access to the database inventory during the time data integrity is being checked using lock-based protocols. The transactions get exclusive locks on the inventory item that is about to be purchased to allow only the particular transaction to modify the quantity of the item at a time.
  • Banking Transactions: In a banking system, customers may be doing tasks at the same time, like fund transfers or account withdrawals. A lock-based protocol in DBMS is in place to make sure that a customer’s account balance is edited by only one transaction at a time to ensure data coherence and not to prevent an overdraft or false balance in a transaction.

Final Words

Lock-based protocols in DBMS are the main instruments in database management systems (DBMS) for providing data integrity, concurrency control, and consistency in multithreaded environments. By maintaining the rights to shared resources and docks, such protocols make it possible to operate transactions concurrently without losing consistency and without the risk of conflicts.

There are a variety of lock-based protocols in use that are the backbone of many database systems. The lock-based protocol helps to maintain integrity and support concurrent access to shared data resources in modern applications. By knowing the principles, practices, and real-world performance of lock-based protocols, developers can construct powerful and reliable systems that address the persistent and changing demands posed by dynamic and intelligent environments.

FAQs

What is a lock-based protocol in DBMS?

A lock-based protocol for the database management system is a concurrency control mechanism that makes sure shared objects (like database data fields) can be accessed by the transactions only after they are locked.

How many types of locks are there in DBMS?

In DBMS, there are typically two main types of locks: Shared Locks (S-Locks) and Exclusive Locks (X-Locks).

What is the index locking protocol in DBMS?

The index locking protocol in DBMS means that locks are being applied to index structures to ensure exclusive access to indexed data when concurrent processes are occurring.

What is the 2PL protocol in DBMS?

The Two-Phase Locking (2PL) protocol in DBMS is a concurrency control mechanism where transactions follow two phases: both elongation and retraction stages.

What are database lock types?

Database lock types cover a spectrum of lock granularity in the database. These lock types include Database-level locks, Table-level locks, Row-level locks, Page-level locks, and Field-level locks.

What are the two types of database locks?

The two types of database locks are Shared Locks and Exclusive Locks.

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. .