You are on page 1of 40

Chapter 16: Concurrency

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

• A transaction may be granted a lock on an item if the requested


lock is compatible with locks already held on the item by other
transactions
• 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.
Let A & B be two accounts, accessed by T1 and T2. T1
transfers Rs. 50 from account B to A. Initial values of A=
Rs.100 and B= Rs.200 and is defined as:

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)

If these transactions are performed serially then T2 will


display the value Rs. 300
In case the transactions are executed concurrently, T2
displays Rs.250 because T1 unlocked data item B too early
which caused inconsistency.
Concurrency control manager
T1 T2
Lock-X(B)
Grant –X(B,T1)
Read(B,b)
b:=b-50
Write(B,b)
Unlock(B)
Lock-S(A) Grant-S(A,T2)
Read(A,a)
Unlock(A)
Lock-S(B)
Grant-S(B,T2)
Read(B,b)
Unlock(B)
Display(a+b)
Lock-X(A) Grant-X(A,T2)
Read(A,a)
a:=a+50
Write(A,a)
Unlock(A)
Lock-Based Protocols (Cont.)
• Locking as above is not sufficient to guarantee serializability
— if A and B get updated in-between the read of A and B,
the displayed sum would be wrong.
• 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

• 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.

• To handle a deadlock one of T3 or T4 must be rolled back


and its locks released.
Pitfalls of Lock-Based Protocols
(Cont.)
• 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:
• 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.
• The same transaction is repeatedly rolled back due to deadlocks.
• Concurrency control manager can be designed to prevent
starvation.
The Two-Phase Locking
Protocol
• This is a protocol which ensures conflict-serializable schedules.
• Phase 1: Growing Phase
• transaction may obtain locks
• transaction may not release locks
• Phase 2: Shrinking Phase
• transaction may release locks
• transaction may not obtain locks
• The protocol assures serializability. It can be proved that the
transactions can be serialized in the order of their lock points
(i.e. the point where a transaction acquired its final lock).
The Two-Phase Locking Protocol (Cont.)
Problems with two-phase locking protocols :
• Two-phase locking does not ensure freedom from deadlocks
• Cascading roll-back is possible under two-phase locking.
T3 T4 T5
Lock-X(A)
Read(A,a)
Lock-S(B)
Read(B,b)
Write(A,a)
Unlock(A)
Lock-X(A)
Read(A,a)
Write(A,a)
Unlock(A)
Lock-S(A)
Read(A,a)
Rollback
Solutions to avoid cascading
rollback:
Follow a modified protocol called strict two-phase
locking. Here a transaction must hold all its exclusive
locks till it commits/aborts. Using this method schedule
will be recoverable and cascade less.
Rigorous two-phase locking is even stricter: here all
locks are held till commit/abort. In this protocol
transactions can be serialized in the order in which they
commit.
T1
Lock-s(A)
Read(A)
Lock-x(B)
Read(B)
Write(B)
commit
Unlock(B)
Unlock(A)

2 PL: There is growing and shrinking phase so it is 2 PL.


Strict 2 PL: Exclusive locks are unlocked after commit. So
yes it is.
Rigorous: We have unlocked all the locks after commit so
it is rigorous.
T1
Lock-s(A)
Read(A)
Lock-x(B)
Unlock(A)
Read(B)
Write(B)
commit
Unlock(B)
Conversion of locks:
consider the example
T8 T9
Lock-X(A) Lock-S(A)
Read(A)
Write(A)

Now, if T9 wants to just read A it has to wait for T8 to


unlock(A).
It is better if T8 could initially lock A in shared mode and
later change to Exclusive mode when write is to be
performed.
Upgrading- converting S to X(only in growing phase)
Downgrading- converting X to S(only in shrinking
phase)
Timestamp-Based Protocols
• Each transaction is issued a timestamp when it enters the
system.

• If an old transaction Ti has time-stamp TS(Ti), a new transaction


Tj is assigned time-stamp TS(Tj) such that TS(Ti) <TS(Tj).

• The protocol manages concurrent execution such that the time-


stamps determine the serializability order.
Timestamp-Based Protocols

There are two simple methods for implementing this scheme:

1.Use the value of the system clock as the timestamp;

2.Use a logical counter that is incremented after a new


timestamp has been assigned;

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

T1 (10) T2(20) T1 (20) T2(10)


R(A) R(A)
W(A) W(A)
R(B) R(B)
W(B) W(B)

Allowed to Not Allowed to


Execute T1 T2 Execute T1 T2
In order to assure such behavior, the protocol
maintains for each data Q two timestamp values:

W-timestamp(Q) is the largest time-stamp of any


transaction that executed write(Q) successfully.

R-timestamp(Q) is the largest time-stamp of any


transaction that executed read(Q) successfully.

These timestamps are updated whenever a new read(Q) or


write(Q) instruction is executed.
Timestamp-Based Protocols
(Cont.)
• The timestamp ordering protocol ensures that any conflicting read and write
operations are executed in timestamp order.
• Suppose a transaction Ti issues a read(Q)
1. If TS(Ti)  W-timestamp(Q), then Ti needs to read a value of Q
that was already overwritten. Hence, the read operation is
rejected, and Ti is rolled back.

(eg. Ti has a timestamp 4:00pm and W-timestamp(Q) is 4:02pm it means that if a


transaction with timestamp 4:00pm want to read the data item which is already written
by transaction of timestamp 4:02,read request of 4:00pm transaction is rejected and
TS(Ti ) is rolled back).
2. If TS(Ti) W-timestamp(Q), then the read operation is
executed, and R-timestamp(Q) is set to the maximum
of R- timestamp(Q) and TS(Ti).

(eg. If a transaction of timestamp 4:05pm issues instruction


to read a data item which is written by a transaction of
timestamp 4:02,then read operation is allowed and R-
timestamp for Q data item set equal to 4:05pm because
transaction of timestamp 4:05pm performs the read
operation successfully.)
Timestamp-Based Protocols
(Cont.)
Suppose that transaction Ti issues write(Q).
1. If TS(Ti) < R-timestamp(Q), then the value of Q that Ti is
producing was needed previously, and the system assumed that
that value would never be produced. Hence, the write
operation is rejected, and Ti is rolled back.

(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)

3. Otherwise, the write operation is executed, and W-


timestamp(Q) is set to TS(Ti).

(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

• The timestamp-ordering protocol guarantees serializability since


all the arcs in the precedence graph are of the form:

transaction transaction
with smaller with larger
timestamp timestamp

Thus, there will be no cycles in the precedence graph


• Timestamp protocol ensures freedom from deadlock as no
transaction ever waits.
• But the schedule may not be cascade-free, and may not even be
recoverable.
Recoverability and Cascade
Freedom
• Problem with timestamp-ordering protocol:
• Suppose Ti aborts, but Tj has read a data item written by Ti
• Then Tj must abort; if Tj had been allowed to commit earlier, the
schedule is not recoverable.
• Further, any transaction that has read a data item written by Tj
must abort
• This can lead to cascading rollback --- that is, a chain of rollbacks
• Solution:
• A transaction is structured such that its writes are all performed at
the end of its processing
• All writes of a transaction form an atomic action; no transaction
may execute while a transaction is being written
• A transaction that aborts is restarted with a new timestamp
Deadlock Handling
• There are two principal methods for dealing with deadlock
problem.
1.Deadlock prevention & avoidance protocols ensure that the
system will never enter into a deadlock state. Some prevention
strategies :
• Require that each transaction locks all its data items before it begins
execution (pre-declaration).
2.Deadlock Detection and Recovery: We can allow the system to
enter into deadlock state and then try to recover by using a
deadlock detection and recovery.

*Detection and Recovery requires overhead that includes not only


the run time cost but also the potential losses inherent in recovery
from a deadlock.
Deadlock Prevention:
• Removing mutual exclusion: All data items must be sharable that
means at a time more than one transaction can get a hold of the data
items. That approach is practically impossible.

• Removing hold and wait condition: This can be removed if the


transaction acquires all the data items that are needed before starting
out. Another way to remove this to enforce a rule of requesting data
item when there are none in held by the transaction.

• Preemption of resources: Preemption of data items from a


transaction can result in rollback and thus this needs to be avoided in
order to maintain the consistency and stability of the system.

• Avoid circular wait condition: This can be avoided if the data


items are maintained in a hierarchy and transaction can hold the data
items in increasing order of precedence. This avoid circular wait.
Another way of doing this to force one data item per transaction rule
– A transaction can request for a data item once it releases the data
item currently being held by it. This avoids the circular wait.
Deadlock Prevention Strategies
Deadlock Avoidance

• Following schemes use transaction timestamps for the sake of


deadlock prevention alone.
• wait-die scheme — non-preemptive
• When transaction Ti requests a data item currently held by Tj, Ti is
allowed to wait only if it has a timestamp smaller than that of Tj (Ti is
older than Tj). Otherwise, Ti is rolled back (dies).
• wound-wait scheme — preemptive
• When transaction Ti requests a data item currently held by Tj, Ti is
allowed to wait only if it has a timestamp larger than that of Tj (Ti is
younger than Tj). Otherwise, Tj is rolled back (Tj is wounded by Ti).

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.)

Wait-for graph without a cycle


Wait-for graph with a

cycle

T26T28T27T26
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

1. Read and execution phase: Transaction Ti writes only to


temporary local variables
2. Validation phase: Transaction Ti performs a ``validation test''
to determine if local variables can be written without violating
serializability.
3. Write phase: If Ti is validated, the updates are applied to the
database; otherwise, Ti is rolled back.
• The three phases of concurrently executing transactions can be
interleaved, but each transaction must go through the three phases in
that order.
• Assume for simplicity that the validation and write phase occur together,
atomically and serially
• I.e., only one transaction executes validation/write at a time.
• Also called as optimistic concurrency control since transaction
executes fully in the hope that all will go well during validation
Validation-Based Protocol
(Cont.)
• Each transaction Ti has 3 timestamps
• Start(Ti) : the time when Ti started its execution
• Validation(Ti): the time when Ti entered its validation phase
• Finish(Ti) : the time when Ti finished its write phase
• Serializability order is determined by timestamp given at
validation time, to increase concurrency.
• Thus TS(Ti) is given the value of Validation(Ti).
• This protocol is useful and gives greater degree of concurrency if
probability of conflicts is low.
• because the serializability order is not pre-decided, and
• relatively few transactions will have to be rolled back.
Validation Test for
Transaction Tj
• If for all Ti with TS (Ti) < TS (Tj) either one of the following
condition holds:
• finish(Ti) < start(Tj)
• start(Tj) < finish(Ti) < validation(Tj) and the set of data items written
by Ti does not intersect with the set of data items read by Tj.
then validation succeeds and Tj can be committed. Otherwise,
validation fails and Tj is aborted.
• Justification: Either the first condition is satisfied, and there is no
overlapped execution, or the second condition is satisfied and
 the writes of Tj do not affect reads of Ti since they occur after Ti has
finished its reads.
 the writes of Ti do not affect reads of Tj since Tj does not read any
item written by Ti.
Schedule Produced by
Validation
• Example of schedule produced using validation

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

• Allow data items to be of various sizes and define a hierarchy of


data granularities, where the small granularities are nested within
larger ones.

• Can be represented graphically as a tree .

• When a transaction locks a node in the tree explicitly, it implicitly


locks all the node's descendants in the same mode.
Example of Granularity
Hierarchy

• The highest level in the example hierarchy is the entire


database.
• The levels below are of type area, file and record in that order.
End of Chapter

You might also like