Concurrency Control Assignment
Concurrency Control Assignment
CONCURRENCY
CONTROL
By
Abhijeet Kumar Nath
And
Nilesh Robin Manickam
02
Lock-Based Protocols
Timestamp-Based Protocols
Validation-Based Protocols
Multiple Granularity
Multiversion Schemes
Deadlock Handling
Insert and Delete Operations
Concurrency in Index Structures
03
LOCK-BASED PROTOCOLS
A lock is a mechanism to control concurrent access to a data item
Data items can be locked in two modes :
1. exclusive (X) mode:- Data item can be both read as well as written. X-
lock is requested using lock-X instruction
2. shared (S) mode:- Data item can only be read. S-lock is requested using
lock-S instruction.
Any number of transactions can hold shared locks on an item, but if any transaction
holds an exclusive on the item no other transaction may hold any lock on the item.
If a lock cannot be granted, the requesting transaction is made to wait till all
incompatible locks held by other transactions have been released. The lock is then
granted.
Lock-Based Protocols (Cont.)
Example of a transaction performing locking:
A locking protocol is a set of rules followed by all transactions while requesting and
releasing locks. Locking protocols restrict the set of possible schedules.
Pitfalls of Lock-Based Protocols
Consider the partial schedule
The potential for deadlock exists in most locking protocols. Deadlocks are
a necessary evil.
Starvation is also possible if concurrency control manager is badly designed.
For example:
1. A transaction may be waiting for an X-lock on an item, while a sequence of other
transactions request and are granted an S-lock on the same item.
2. The same transaction is repeatedly rolled back due to deadlocks.
– First Phase:
– Second Phase:
if Ti has a lock on D
then
read(D)
else
begin
if necessary wait until no
other transaction has a lock-X on D
grant Ti a lock-S on D; read(D)
end
Automatic Acquisition of Locks (Cont.)
if Ti has a lock on D
then
read(D)
else
begin
if necessary wait until no
other transaction has a lock-X on D
grant Ti a lock-S on D; read(D)
end
Automatic Acquisition of Locks (Cont.)
write(D) is processed as:
if Ti has a lock-X on D
then
write(D)
else
begin
if necessary wait until no other trans. has any lock on D,
if Ti has a lock-S on D
then
upgrade lock on D to lock-X
else
grant Ti a lock-X on D
write(D)
end;
All locks are released after commit or abort
Implementation of Locking
A Lock manager can be implemented as a separate process to
which transactions send lock and unlock requests
The lock manager replies to a lock request by sending a lock grant
messages (or a message asking the transaction to roll back, in
case of a deadlock)
The requesting transaction waits until its request is answered
The lock manager maintains a datastructure called a lock table to
record granted locks and pending requests
The lock table is usually implemented as an in-memory hash
table indexed on the name of the data item being locked
Lock Table
Black rectangles indicate granted locks, white ones indicate waitin
requests
Lock table also records the type of lock granted or requested
New request is added to the end of the queue of requests for the
data item, and granted if it is compatible with all earlier locks
Unlock requests result in the request being deleted, and later
requests are checked to see if they can now be granted
If transaction aborts, all waiting or granted requests of the
transaction are deleted
Impose a partial ordering → on the set D = {d1, d2 ,..., dh} of all data
items.
1. If di → dj then any transaction accessing both di and dj must access di before accessing
dj
2. Implies that the set D may now be viewed as a directed acyclic graph, called a database
graph..
The first lock by Ti may be on any data item. Subsequently, a data Q can be
locked by Ti only if the parent of Q is currently locked by Ti.
In order to assure such behavior, the protocol maintains for each data Q two
timestamp values:
1. W-timestamp(Q) is the largest time-stamp of any transaction that executed write(Q)
successfully.
2. R-timestamp(Q) is the largest time-stamp of any transaction that executed read(Q)
successfully
Timestamp-Based Protocols (Cont.)
The timestamp ordering protocol ensures that any conflicting read and
write operations are executed in timestamp order.
The compatibility
matrix for all lock
modes is:
Multiple Granularity Locking Scheme
Transaction Ti can lock a node Q, using the following rules:
1. The lock compatibility matrix must be observed.
2. The root of the tree must be locked first and may be locked in any mode.
3. A node Q can be locked by Ti in S or IS mode only if the parent of Q
is currently locked by Ti in either IX or IS mode.
4. A node Q can be locked by Ti in X, SIX, or IX mode only if the parent of Q
currently locked by Ti either IX or SIX modes.
5. Ti can lock a node only if it has not previously unlocked any node (that is,
Ti is two-phase).
6. Ti can unlock a node Q only if none of the children of Q are currently
locked by Ti.
Observe that locks are acquired in root-to-leaf order, whereas they are
released in leaf-to-root order.
Multiversion Schemes Multiversion Schemes
Multiversion schemes keep old versions of data item to increase
concurrency.
Multiversion Timestamp Ordering
Multiversion Two-Phase Locking
Each successful write results in the creation of a new version of
the data item written.
Use timestamps to label versions.
When a read(Q) operation is issued, select an appropriate
version of Q based on the timestamp of the transaction, and
return the value of the selected version.
Reads never have to wait as an appropriate version is returned
immediately.
Multiversion Timestamp Ordering