Chapter 16: Concurrency Control
Chapter 16: Concurrency Control
Chapter 16: Concurrency Control
Control
• Lock-Based Protocols
• Timestamp-Based Protocols
• Validation-Based Protocols
• Multiple Granularity
• Multiversion Schemes
• Deadlock Handling
• Insert and Delete Operations
Concurrency control:
Managing simultaneous execution of
transactions in a database to ensure
serializability.
Need:
Enforce isolation
Preserve database consistency
Resolve RW and WW conflicts
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.
• Lock requests are made to concurrency-control manager.
Transaction can proceed only after request is granted.
Lock-Based Protocols (Cont.)
• Lock-compatibility matrix
T1 T2
Lock-X(B) Lock-S(A)
Read(B,b) Read(A,a)
B:=b-50 Unlock(A)
Write(B,b) Lock-S(B)
Unlock(B) Read(B,b)
Unlock(B)
Lock-X(A) Display(a+b)
Read(A,a)
a:=a+50
Write(A,a)
Unlock (A)
• Neither T3 nor T4 can make progress — executing lock-S(B) causes T4 to wait for T3 to
release its lock on B, while executing lock-X(A) causes T3 to wait for T4 to release its lock
on A.
• Such a situation is called a deadlock.
• System is deadlocked if there is a set of transactions such that every transaction in the set is
waiting for another transaction in the set.
Thus, if TS(Ti) < TS(Tj), then the system must ensure that
the produced schedule is equivalent to a serial schedule in
which transaction Ti appears before transaction Tj.
Examples
(For eg-if a transaction with timestamp 3.58 issue to write a data item
whose R-timestamp is 4.00pm,then it means TS(Ti) wants to write a
data item which is successfully read by a transaction of higher
timestamp then the write operation is rejected and Ti is rolled back.)
2. If TS(Ti) < W-timestamp(Q), then Ti is attempting to write
an obsolete value of Q. Hence, this write operation is
rejected, and Ti is rolled back.
(For eg- if TS(Ti) issues an instruction to write a data item with timestamp
3.58 which is successfully written by a transaction whose timestamp is
4.00pm,then it means that data item is already written by higher
timestamp transaction so the write operation of transaction having 3.58
timestamp is rejected and TS(Ti) is rolled back)
(For eg- if TS(Ti) timestamp is greater the 4:00pm and R-timestamp and
W-timestamp less than TS(Ti) then the transaction can perform the write
operation without any problem.)
Correctness of Timestamp-Ordering Protocol
transaction transaction
with smaller with larger
timestamp timestamp
The major problem with both the schemes that unnecessary rollbacks
may occur.
Here is the table representation of resource allocation for each algorithm.
Both of these algorithms take process age into consideration while
determining the best possible way of resource allocation for deadlock
avoidance.
Deadlock prevention
(Cont.)
• Timeout-Based Schemes :
• a transaction waits for a lock only for a
specified amount of time. After that, the wait
times out and the transaction is rolled back.
• thus deadlocks are not possible
• simple to implement; but starvation is possible.
Also difficult to determine good value of the
timeout interval.
Deadlock Detection
• Deadlocks can be described as a wait-for graph, which consists of a
pair G = (V,E),
• V is a set of vertices (all the transactions in the system)
• E is a set of edges; each element is an ordered pair Ti Tj.
• If Ti Tj is in E, then there is a directed edge from Ti to Tj,
implying that Ti is waiting for Tj to release a data item.
• When Ti requests a data item currently being held by Tj, then the
edge Ti Tj is inserted in the wait-for graph. This edge is removed
only when Tj is no longer holding a data item needed by Ti.
• The system is in a deadlock state if and only if the wait-for graph
has a cycle. Must invoke a deadlock-detection algorithm
periodically to look for cycles.
Deadlock Detection
(Cont.)
cycle
T26T28T27T26
Deadlock Recovery
• When deadlock is detected :
• Some transaction will have to rolled back (made a victim) to break
deadlock. Select that transaction as victim that will incur minimum
cost.
Some of the methods used for victim selection are −
• Choose the youngest transaction.
• Choose the transaction with fewest data items.
• Choose the transaction that has performed least number of updates.
• Choose the transaction which is common to two or more cycles.
Rollback -- determine how far to roll back transaction
• Total rollback: Abort the transaction and then restart it.
• More effective to roll back transaction only as far as necessary to break
deadlock.
• Starvation happens if same transaction is always chosen as victim.
Include the number of rollbacks in the cost factor to avoid starvation
Validation-Based Protocol
• Execution of transaction T is done in three phases.
i
T14 T15
read(B)
read(B)
B:- B-50
read(A)
A:- A+50
read(A)
(validate)
display (A+B)
(validate)
write (B)
write (A)
Multiple Granularity