Chapter 15 - Concurrency
Chapter 15 - Concurrency
Control
BOOK REFERRED : CHAPTER 15 (KORTH, 6 T H EDITION)
Concurrency
Allowing many transactions to access the same database at the same time.
Some kind of control mechanism is needed to ensure that concurrent transactions do not
interfere with each other.
The goal is to develop concurrency control protocols that will assure serializability.
Three Concurrency Problems
1. Lost Update
2. Uncommitted Dependency
3. Inconsistent Analysis
It is the logical possibility of each of these that demands the need for concurrency control. The
problems arise from two or more transactions reading or writing on the same part of the same
database.
Lost Update Problem
Time User 1 (Trans A) User2 (Trans B)
1 Retrieve t
2 Retrieve t
3 Update t
4 Update t
5
6
7
1 Update t
2 Retrieve t
3 Rollback
4
5
6 Update t
7 Update t
8 Rollback
Here are two versions of this problem. In each one (Times 1-3 and 6-8) Transaction A
is dependent on an uncommitted change made by Transaction B which is lost on
Rollback.
Inconsistent Analysis
Initially: Acc1 = 40; Acc2 = 50; Acc3 = 30;
Time User 1 (Trans A) User 2 (Trans B)
1 Retrieve Acc 1 :
Sum = 40
2 Retrieve Acc2 :
Sum = 90
3 Retrieve Acc3 :
4 Update Acc3:
30 → 20
5 Retrieve Acc1:
6 Update Acc1:
40 → 50
7 commit
8 Retrieve Acc3:
Sum = 110 (not 120)
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.
Lock-Based Protocols (Cont.)
Example of a transaction performing locking:
T2: lock-S(A); T1: lock-X(B);
read(B);
read (A); B := B − 50;
unlock(A); write(B);
unlock(B);
lock-S(B);
lock-X(A);
read (B); read(A);
A := A + 50;
unlock(B);
write(A);
display(A+B) unlock(A)
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.
Lost Update ‘solved’
Time User 1 (Trans A) User2 (Trans B)
1 Retrieve t (get S-lock on t)
2 Retrieve t (get S-lock on t)
3 Update t (request X-lock on t)
4 wait Update t (request X-lock on t)
5 wait wait
6 wait wait
7
No update is lost but the result is deadlock – see later how to deal with this.
Uncommitted Dependency
‘solved’
Time User 1 (Trans A) User 2 (Trans B)
A resolution of the first version of the problem. Second version is similar (check this!).
Inconsistent Analysis ‘solved’
Time User 1 (Trans A) User 2 (Trans B)
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.
◦ To handle a deadlock one of T3 or T4 must be rolled back
and its locks released.
Pitfalls of Lock-Based Protocols
T1 : Lock - X(A) T2 : Lock - S(A) T1 T2
Read(A) Read(A) Lock - X(A)
A = A – 100 Unlock - S(A) Read(A)
Write(A) Lock - S(B) A = A – 100
Unlock - X(A) Read(B) Write(A)
Unlock - X(A)
Lock - X(B) Unlock - S(B)
Read(B) Display A + B
Lock - S(A)
B = B + 100 Read(A)
Write(B) Unlock - S(A)
Unlock - X(B) Lock - S(B)
Consider A = 1000 and B = 500 Read(B)
Unlock - S(B)
Sum of A + B should be preserved i.e. Display A + B
1500, both in serial as well as concurrent
transaction. Hence, the schedule is not
serializable. Lock - X(B)
Read(B)
Problem found : “Locks should be kept till B = B + 100
the end, unlocking early creates Write(B)
Unlock - X(B)
inconsistency”.
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
– Second Phase:
◦ can release a lock-S
◦ can release a lock-X
◦ can convert a lock-X to a lock-S (downgrade)
Concept of Locking Protocol
Let {T0, T1, . . . , Tn} be a set of transactions participating in a schedule S. We say that Ti
precedes Tj in S, written Ti → Tj, if there exists a data item Q such that Ti has held lock mode A
on Q, and Tj has held lock mode B on Q later, and comp(A,B) = false. If Ti →Tj , then that
precedence implies that in any equivalent serial schedule, Ti must appear before Tj .
We say that a schedule S is legal under a given locking protocol if S is a possible schedule for a
set of transactions that follows the rules of the locking protocol.
We say that a locking protocol ensures conflict serializability if and only if all legal schedules are
conflict serializable; in other words, for all legal schedules the associated → relation is acyclic.
Ensuring serializability
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).
T1 T2 T3
LP
LP
LP
Each transaction observes the two-phase locking protocol, but the failure of T5 after the read(A) step of T7
leads to cascading rollback of T6 and T7.
Also, Two-phase locking does not ensure freedom from deadlocks.
Problems with 2-phase locking
protocol
Unnecessary wait due to early lock
Deadlock T1 T2
Cascading rollback
Lock – X(A)
T1 T2 R(A)
W(A)
Lock-X(B)
Lock – X(B)
Read(B) . Lock – S (A)
Write(B) . Has to wait till all
. the operations of
Lock-S(A)
. T1 gets over to
Read(A) release the locks
Lock-X(B)
Lock-X(A) .
. .
. .
The Two-Phase Locking Protocol
Two-phase locking does not ensure freedom from deadlocks.
Cascading roll-back is possible under two-phase locking. To avoid this, follow a modified protocol
called strict two-phase locking. Here a transaction must hold all its exclusive locks till it
commits/aborts. This requirement ensures that any data written by an uncommitted transaction are
locked in exclusive mode until the transaction commits, preventing any other transaction from reading
the data.
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.
2. The wound–wait scheme is a preemptive technique. It is a counterpart to the wait–die scheme. 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 (that is, Ti is younger than Tj ). Otherwise, Tj is rolled back (Tj is wounded by Ti ).
◦ Returning to our example, with transactions T14, T15, and T16, if T14 requests a data item held by T15, then the
data item will be preempted from T15, and T15 will be rolled back. If T16 requests a data item held by T15, then
T16 will wait.
Deadlock Prevention
Strategies
Following schemes use transaction timestamps for the sake of deadlock prevention
alone.
wait-die scheme — non-preemptive
◦ older transaction may wait for younger one to release data item. (older means smaller timestamp) . Younger
transactions never wait for older ones; they are rolled back instead.
◦ a transaction may die several times before acquiring needed data item
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.)
T2 Read(Y)
T1 Write(X)
T2 T3
T2 Read(X)
T3 Read(Z)
Wait for graph
T3 Write(Z)
T1 Read(Y)
T3 Read(X) T1
T1 Write(Y)
T2 T3
Precedence graph
Example
T1 Read(X) T1
T2 Read(Y)
T1 Write(X)
T2 T3
T2 Read(X)
T3 Read(Z)
Wait for graph
T3 Write(Z)
T1 Read(Y)
T3 Read(X) T1
T1 Write(Y)
T2 T3
Precedence graph
Example
T1 Read(X) T1
T2 Read(Y)
T1 Write(X)
T2 T3
T2 Read(X)
T3 Read(Z)
Wait for graph
T3 Write(Z)
T1 Read(Y)
T3 Read(X) T1
T1 Write(Y)
T2 T3
Precedence graph
Example
T1 Read(X) T1
T2 Read(Y)
T1 Write(X)
T2 T3
T2 Read(X)
T3 Read(Z)
Wait for graph
T3 Write(Z)
T1 Read(Y)
T3 Read(X) T1
T1 Write(Y)
T2 T3
Precedence graph
Example
T1 Read(X) read-locks(X) T1
T2 Read(Y) read-locks(Y)
T1 Write(X) write-lock(X)
T2 T3
T2 Read(X) tries read-lock(X)
T3 Read(Z)
Wait for graph
T3 Write(Z)
T1 Read(Y)
T3 Read(X) T1
T1 Write(Y)
T2 T3
Precedence graph
Example
T1 Read(X) read-locks(X) T1
T2 Read(Y) read-locks(Y)
T1 Write(X) write-lock(X)
T2 T3
T2 Read(X) tries read-lock(X)
T3 Read(Z) read-lock(Z)
Wait for graph
T3 Write(Z) write-lock(Z)
T1 Read(Y) read-lock(Y)
T3 Read(X) tries read-lock(X) T1
T1 Write(Y)
T2 T3
Precedence graph
Example
T1 Read(X) read-locks(X) T1
T2 Read(Y) read-locks(Y)
T1 Write(X) write-lock(X)
T2 T3
T2 Read(X) tries read-lock(X)
T3 Read(Z) read-lock(Z)
Wait for graph
T3 Write(Z) write-lock(Z)
T1 Read(Y) read-lock(Y)
T3 Read(X) tries read-lock(X) T1
T1 Write(Y) tries write-lock(Y)
T2 T3
Precedence graph
Deadlock Recovery
When deadlock is detected, three actions can be taken :
1. Selection of a victim. Given a set of deadlocked transactions, we must determine which
transaction (or transactions) to roll back to break the deadlock. We should roll back those
transactions that will incur the minimum cost. Unfortunately, the term minimum cost is not a
precise one. Many factors may determine the cost of a rollback, including:
a. How long the transaction has computed, and how much longer the transaction will compute
before it completes its designated task?
b. How many data items the transaction has used?
c. How many more data items the transaction needs for it to complete?
d. How many transactions will be involved in the rollback?
Deadlock Recovery (Contd…)
When deadlock is detected, three actions can be taken :
2. Rollback. Once we have decided that a particular transaction must be rolled back, we must
determine how far this transaction should be rolled back. The simplest solution is a total rollback:
Abort the transaction and then restart it. However, it is more effective to roll back the transaction only
as far as necessary to break the deadlock. Such partial rollback requires the system to maintain
additional information about the state of all the running transactions.
Deadlock Recovery (Contd…)
When deadlock is detected, three actions can be taken :
3. Starvation. In a system where the selection of victims is based primarily on cost factors, it
may happen that the same transaction is always picked as a victim. As a result, this
transaction never completes its designated task, thus there is starvation. We must ensure that
a transaction can be picked as a victim only a (small) finite number of times. The most
common solution is to include the number of rollbacks in the cost factor.
Thank You!!