Locking in Concurrency Control
A variable that is connected with a data item and specifies the item's current state in relation to the possible operations that could be performed on it is called a lock. The first transaction must finish all of its tasks before a second transaction can access a database record. In most cases, each piece of data in the database has its own lock. The purpose of locks is to maintain concurrent transactions' access to database elements.

Therefore, locking methods are designed to enable compatible operations to be executed sequentially. In other words, acts that can be changed are compatible. For the majority of applications, locking is the preferred approach of concurrency management and is the most commonly used technique.
Types of Locks:
There are two types of locks in Locks based protocols. These are:
1. Binary Locks
These are limited to being either locked or unlocked.
2. Shared or Exclusive Locks
When just read operations are needed, shared locks are obtained. Multiple transactions can use shared locks because no data is being changed. In order to accomplish write operations, exclusive locks are used. The only transaction that may modify the data value is the one that holds an exclusive lock.
Types of Lock-Based Protocols:
1. Simplistic Lock Protocol -
The transaction obtains a lock on the data value before to performing the write operation. The lock must be released after the write operation. A Simplistic Lock Protocol example could look like this:
T1 | T2 |
---|---|
R(A) | |
R(A). | |
Lock(B) | |
R(B) | |
W(B) | |
Unlock(B) | |
Lock(C) | |
R(C) | |
W(C) | |
Unlock(C) | |
Commit | |
Commit | |
Transactions T1 and T2 are displayed above. The read operation does not require locks, but each of these transactions obtains a lock before the write operation and releases it after.
2. Two-Phase Locking Protocol:
The growing and shrinking phases are the two stages of the two-phase locking technique. Only when the transaction is in the growing phase can it gain locks. It can free the locks it has already obtained when it reaches the shrinking phase, but it is unable to obtain additional locks. S stands for the shared locks, and X for the unique locks.
The following is an example of a two-phase locking protocol:
T1 | T2 |
---|---|
S(A) | |
R(A) | |
S(A) | |
R(A) | |
X(B) | |
R(B) | |
W(B) | |
X(C) | |
R(C) | |
W(C) | |
Unlock(C) | |
Unlock(A) | |
Unlock(B) | |
Unlock(A) | |
Commit | |
Commit |
In the example above, T1 and T2 use a shared lock to share the variable A because only read operations are carried out on it. T1 obtains and instantly releases an exclusive lock on B for the write operation. The same is true for C and T2.
3. Strict Two-Phase Locking Protocol
Two-phase locking protocols are similar to strict two-phase locking protocols. The main difference is that in an accurate 2PL protocol, every exclusive lock that the protocol has obtained must be maintained until it either commits or aborts. The following is a concrete example of a strict two-phase locking protocol:
T1 | T2 |
---|---|
S(A) | |
R(A) | |
S(A) | |
R(A) | |
X(B) | |
R(B) | |
W(B) | |
X(C) | |
R(C) | |
W(C) | |
Unlock(A) | |
Unlock(A) | ) |
Commit | |
Unlock(B) | |
Commit | |
Unlock(C) |
In the example above, T1 and T2 use a shared lock to share the variable A because only read operations are carried out on it. On B, T1 obtains an exclusive lock for the write operation, and on C, T2 performs the same. When the transactions are committed, only then are the exclusive locks released. The shared locks, however, do not have such a bound.
4. Rigorous Two-Phase Locking Protocol
Strict two-phase locking protocol and two-phase locking protocol are simply extensions of rigorous two-phase locking protocol. In this case, a transaction's shared and exclusive locks are all released only when the transaction commits or aborts.
Here is an example of a rigorous two-phase locking protocol:
T1 | T2 |
---|---|
S(A) | |
R(A) | |
S(A) | |
R(A) | |
X(B) | |
R(B) | |
W(B) | |
X(C) | |
R(C) | |
W(C) | |
Commit | |
Unlock(A) | |
Unlock(B) | |
Commit | |
Unlock(A) | |
Unlock(C) |
Since only read operations are carried out on variable A in the example above, T1 and T2 share it using a shared lock. For the write operation, T1 obtains an exclusive lock on B, and T2 does the same with C. Only when the transactions have committed are the exclusive and shared locks released.