Locking Techniques for concurrency control
Concurrency control is provided in a database to:
(I) Enforce isolation among transactions.
(ii) Preserve database consistency through consistency preserving execution of transactions.
(iii) Resolve read-write and write-read conflicts.
Various concurrency control techniques are:
- Two-phase locking Protocol
- Time stamp ordering Protocol
- Multi version concurrency control
- Validation concurrency control
Two-Phase Locking Protocol:
Locking is an operation which secures: permission to read, OR permission to write a data item. Two phase locking is a process used to gain ownership of shared resources without creating the possibility of deadlock.
The 3 activities taking place in the two phase update algorithm are:
(i) Lock Acquisition
(ii) Modification of Data
(iii) Release Lock
Two phase locking prevents deadlock from occurring in distributed systems by releasing all the resources it has acquired, if it is not possible to acquire all the resources required without waiting for another process to finish using a lock. This means that no process is ever in a state where it is holding some shared resources, and waiting for another process to release a shared resource which it requires. This means that deadlock cannot occur due to resource contention.
A transaction in the Two Phase Locking Protocol can assume one of the 2 phases:
(i) Growing Phase:
In this phase a transaction can only acquire locks but cannot release any lock. The point when a transaction acquires all the locks it needs is called the Lock Point.
(ii) Shrinking Phase:
In this phase a transaction can only release locks but cannot acquire any.
Time Stamp Ordering Protocol:
A timestamp is a tag that can be attached to any transaction or any data item, which denotes a specific time on which the transaction or the data item had been used in any way. A timestamp can be implemented in 2 ways. One is to directly assign the current value of the clock to the transaction or data item. The other is to attach the value of a logical counter that keeps increment as new timestamps are required.
The timestamp of a data item can be of 2 types:
This means the latest time when the data item X has been written into.
This means the latest time when the data item X has been read from. These 2 timestamps are updated each time a successful read/write operation is performed on the data item X.
Multiversion Concurrency Control:
Multiversion schemes keep old versions of data item to increase concurrency.
Multiversion 2 phase locking:
Each successful write results in the creation of a new version of the data item written. Timestamps are used to label the versions. When a read(X) operation is issued, select an appropriate version of X based on the timestamp of the transaction.
Validation Concurrency Control:
The optimistic approach is based on the assumption that the majority of the database operations do not conflict. The optimistic approach requires neither locking nor time stamping techniques. Instead, a transaction is executed without restrictions until it is committed. Using an optimistic approach, each transaction moves through 2 or 3 phases, referred to as read, validation and write.
(i) During read phase, the transaction reads the database, executes the needed computations and makes the updates to a private copy of the the database values. All update operations of the transactions are recorded in a temporary update file, which is not accessed by the remaining transactions.
(ii) During the validation phase, the transaction is validated to ensure that the changes made will not affect the integrity and consistency of the database. If the validation test is positive, the transaction goes to a write phase. If the validation test is negative, The, transaction is restarted and the changes are discarded.
(iii) During the write phase, the changes are permanently applied to the database.
It can be defined as hierarchically breaking up the database into blocks which can be locked. The Multiple Granularity protocol enhances concurrency and reduces lock overhead. It maintains the track of what to lock and how to lock. It makes easy to decide either to lock a data item or to unlock a data item. This type of hierarchy can be graphically represented as a tree.
Intention Mode Lock
Intention-shared (IS): It contains explicit locking at a lower level of the tree but only with shared locks.
Intention-Exclusive (IX): It contains explicit locking at a lower level with exclusive or shared locks.
Shared & Intention-Exclusive (SIX): In this lock, the node is locked in shared mode, and some node is locked in exclusive mode by the same transaction.