Locking Techniques in DBMS

In the previous article, we had read that, transactions should be in a serializable schedule. But, testing for Serializability is computationally expensive and impractical. Hence, in order to ensure the correct execution of concurrent transactions, a number of concurrency control techniques are applied in DBMS. One of them is use of Locking Techniques.

Concurrency Control Schemes are the schemes which are used by DBMS to ensure that concurrent transactions do not interfere with each other. Some of these Locking Techniques are:

  • Locking
  • Time Stamps, and
  • Optimistic Protocols.

Here in this article, we will discuss about locking.

Introduction to Locking

Locking is one of themost commonly used concurrency control schemes in DBMS. This works by associating a variable lock on the data items. This variable describes the status of the data item with respect to the possible operations that can be applied on it. The value of this variable is used to control the concurrent access and the manipulation of the associated data item.

The concurrency control technique in which the value of the lock variable is manipulated is called locking. The technique of locking is one way to ensure Serializability in DBMS.

In DBMS, locking is the responsibility of a subsystem called lock manager.

Types of Locking Techniques

To control concurrency there are various types of locks which can be applied in DBMS.

Locking Techniques in DBMS

Locks are usually issued by the transactions before any operations are performed by them.

Binary Locks

A binary lock has two states or values associated with each data item. These values are:

  1. Locked – 1
  2. Unlocked – 0

If a data item is locked, then it cannot be accessed by other transactions i.e., other transactions are forced to wait until the lock is released by the previous transaction.

But, if a data item is in the unlocked state, then, it can be accessed by any transaction and on access the lock value is set to locked state.

These locks are applied and removed using Lock () and Unlock () operation respectively.

In binary locks,at a particular point in time, only one transaction can hold a lock on the data item.Noother transaction will be able to access the same data concurrently. Hence, Binary locks are very simple to apply but are not used practically.

Shared / Exclusive Locks

In shared locks, multiple users are allowed to access the same data item with a read lock which is shared by them. But, in case when a transaction needs to write a data item, then an exclusive lock is applied on that data item. So here, we classify the locks as:

  • Shared Locks
  • Exclusive Locks

Shared Locks

Shared locks are applied to a data item when the transaction requests a read operation on the data item. A shared lock will allow multiple transactions to only read the data item concurrently.

As these locks are applied on read operation, they will not compromise on the consistency of the database.

Exclusive Locks

Exclusive locks on the other hand are applied on the transactions which request a write operation on the data item.

The transaction which is modifying the data item requests an exclusive lock on the data item and hence any other transaction which needs access to the data item has to wait until the lock applied by the previous transaction has been released by it.

But when exclusive locks are applied there are situations when a transaction enters into a wait state indefinitely. Such a state where a transaction cannot come out of the wait state is known as a deadlock.

Two Phase Locking

The Two Phase Locking Techniques guarantee Serializability in DBMS. A transaction is said to follow Two Phase Locking Protocol if all locking operations in the transaction precede the first unlock operation.

In this, locks are applied in two phases:

  • Growing Phase
  • Shrinking Phase

Growing Phase

This phase is also known as the first phase or the expanding phase. It is in this phase that the transaction acquires all the locks needed by it but it cannot release any locks here.

Shrinking Phase

This phase is also known as the second phase or the contracting phase. Here a transaction is not allowed to acquire any new locks but it can release the existing locks it holds. The Two Phase Locking Protocol helps solve problems of lost update, inconsistent analysis or dirty read too.

Be First to Comment

Leave a Reply

Your email address will not be published.