Lock-Based Concurrency Control
Lock-Based Concurrency Control
Control
Ashima Tyagi
Assistant Professor
Computer Science and Engineering
Introduction
• There are instances where more than one user may attempt to access the
same data item simultaneously, resulting in concurrency.
1. Shared Lock
• Shared Locks, which are often denoted as lock-S(), is defined as locks that
provide Read-Only access to the information associated with them.
• Whenever a shared lock is used on a database, it can be read by several users,
but these users who are reading the information or the data items will not have
permission to edit it or make any changes to the data items
Example of Shared Locks: Consider the situation where the value of
variable X equals 50 and there are a total of 2 transactions reading X. If one
transaction wants to change the value of A, another transaction that tries to
read the value will read the incorrect value of the variable X. However, until
it is done with reading, the Shared lock stops it from updating.
A vital point to remember when using Lock-based protocols in Database Management System is
that a Shared Lock can be held by any amount of transactions. On the other hand, an Exclusive
Lock can only be held by one transaction in DBMS, this is because a shared lock only reads data but
does not perform any other activities, whereas an exclusive lock performs read as well as writing
activities.
The two methods outlined below can be used to convert between the locks:
• Growing Phase: In this phase, we can acquire new locks on data items,
but none of these locks can be released.
• Shrinking Phase: In this phase, the existing locks can be released, but no
new locks can be obtained.
Two-phase locking helps to reduce the amount of concurrency in a schedule
but just like the two sides of a coin two-phase locking has a few cons too.
The protocol raises transaction processing costs and may have unintended
consequences. The likelihood of establishing deadlocks is one bad result.
Steps Transaction T1 Transaction T2
0 Lock–S(A)
1 Lock–S(A)
2 Lock–X(B)
3 — —
4 Unlock(A)
5 Lock–X(C)
6 Unlock(B)
7 Unlock(A)
8 Unlock(C)
9 — —
Here,
Lock–S(A): can’t execute Lock–S(A), since A is locked by transaction T1.
Lock–X(C): can’t execute Lock–X(C), since C is locked by transaction T2.
In the above example, the following phase may happen if the lock conversion is allowed:
• Upgrading of the lock is allowed in the growing phase( from S(A) to X(A) ).
• Downgrading of the lock must be done in the shrinking phase( from X(A) to S(A) ).
The following way shows how locking and unlocking happen in two-phase locking(2PL).
In transaction T1:
Growing phase: from steps 1 to 3
Shrinking phase: from steps 5 to 7
Lock point: at step 3
In transaction T2:
Growing phase: from steps 2 to 6
Shrinking phase: from steps 8 to 9
Lock point: at step 6
2. Strict Two-Phase Locking Protocol
In DBMS, Cascaded rollbacks are avoided with the concept of a Strict Two-Phase
Locking Protocol. This protocol necessitates not only two-phase locking but also
the retention of all exclusive locks until the transaction commits or aborts. The
two-phase is with deadlock.
The only difference between strict 2PL and 2PL is that strict two-phase locking does
not release a lock after using it. On the other hand, it waits for the whole
transaction to complete, and then it releases all the locks.
3. Rigorous Two-Phase Locking Protocol
Rigorous two-phase locking protocols avoid cascading rollbacks.
In this, all the shared and exclusive locks are only released when the whole
transaction completes.
Deadlock
Now, the main problem arises. Transaction T1 will wait for transaction T2 to give up
the lock, and similarly, transaction T2 will wait for transaction T1 to give up the lock.
As a consequence, All activity comes to a halt and remains at a standstill forever
unless the DBMS detects the deadlock and aborts one of the transactions.
Deadlock Detection: When a transaction waits indefinitely to obtain a lock,
The database management system should detect whether the transaction is
involved in a deadlock or not.
Wait-for-graph is one of the methods for detecting the deadlock situation. This
method is suitable for smaller databases. In this method, a graph is drawn
based on the transaction and its lock on the resource. If the graph created has a
closed loop or a cycle, then there is a deadlock.
For the above-mentioned scenario, the Wait-For graph is drawn below:
Deadlock prevention mechanism proposes two schemes:
The number of aborts and rollbacks In this, the number of aborts and
is higher in these techniques. rollback is lesser.
Starvation
When a transaction must wait an unlimited period for a lock, it is referred to as
starvation. The following are the causes of starvation :