5 Concurrency Control
5 Concurrency Control
1
concurrency control mechanism. With locking, any data that is retrieved by a user for updating
must be locked, or denied to other users, until the update is complete.
In a binary locking mechanism, an object can either be on a lock state or in an unlock state.
In Shared/ Exclusive locking mechanism, the DBMS uses separate locks for read and write
operations. A read operation is given a shared lock while a write operation uses an exclusive
lock.
Exclusive Lock (X). It is type of lock established on a database object which cannot be shared
within all active Transactions. It is dedicated to particular transaction; only that particular
transaction can access and modify that object. To obtain the exclusive lock on a database
objects, it should not be exclusively locked by any one of the other transactions.
Locking Protocols
It is the set of rules to be followed by each transaction, to ensure that the net effect of execution
of each transaction in an interleaved schedule will be same as, the result obtained when the
transactions executed serially.
1. Two-Phase Locking Protocol (2PL)
2PL protocol ensures serializability by applying a lock to an object thereby blocking other
transactions from accessing the same object simultaneously.
Variants of the 2PL protocol are Strict 2PL Protocol, Rigorous 2PL Protocol, and Conservative
2PL Protocol
i. Strict 2PL Protocol
Strict Two-Phase Locking (Strict 2PL) is the most widely used locking protocol. It
provides few rules to the transactions to access the database objects.
Rule 1: If a Transaction “T” wants to read, and modify an object, it first requests a
shared, and an exclusive lock on the database object respectively.
Rule 2: All Locks held by the transaction will be released when it is completed. All
exclusive-locks are held until the transaction commits or aborts.
Example
If a person updates his bank account, then the database will lock that database object
exclusively to avoid RW conflicts. So, the transactions requesting to read that database
object will be suspended until the updating transaction is concluded.
2
Strict Two-Phase Locking Protocol avoids cascaded rollbacks. However, it is not
deadlock free.
5.3 Deadlock
Deadlock occurs within the transactions in DBMS system. Due to this neither one of the
transactions involved is committed. DBMS has to use suitable recovery systems to overcome
deadlocks.
Deadlock occurs mainly due to the Lock- Based Concurrency Control. The exclusive lock type
will isolate one particular database object from the access of other transactions. This will
3
suspend all other transactions from requesting a shared lock on that particular object until the
transaction holding the exclusive lock on the object is completed. This may create a loop in
database leading to deadlocks within transactions.
5.3.3 Starvation
Starvation is the situation when a transaction needs to wait for an indefinite period in order to
acquire a lock. This happens because:
i. the waiting scheme for locked items is not properly managed
ii. resource leaks
iii. the same transaction is selected as a victim repeatedly by the deadlock recovery
mechanism