Professional Documents
Culture Documents
Concurrency Control
Contents
• Concurrency Control
• Concurrency Control by Locks
• Concurrency Control by Timestamps
• Concurrency Control by Validation
Concurrency Control
T1 T2 … Tn
DB
(consistency
constraints)
T1 T2
READ(A,t) READ(A,s)
t:=t+100 s:=s*2
WRITE(A,t) WRITE(A,s)
READ(B,t) READ(B,s)
t:=t+100 s:=s*2
WRITE(B,t)
WRITE(B,s)
Schedule A
A B
T1 T2 25 25
Read(A); A A+100
Write(A); 125
Read(B); B B+100;
Write(B);
125
Read(A);A A2;
Write(A);
250
Read(B);B B2;
Write(B);
250
250 250
Serial schedule in which T1 precedes T2
Schedule B
A B
T1 T2 25 25
Read(A);A A2;
Write(A); 50
Read(B);B B2;
Write(B); 50
Read(A); A A+100
Write(A); 150
Read(B); B B+100;
Write(B);
150
150 150
Serial schedule in which T2 precedes T1
Schedule C
A B
T1 T2 25 25
Read(A); A A+100
Write(A); 125
Read(A);A A2;
Write(A); 250
Read(B); B B+100;
Write(B);
125
Read(B);B B2;
Write(B);
250
250 250
A serializable, but not serial, schedule
Schedule D
A B
T1 T2 25 25
Read(A); A A+100
Write(A);
125
Read(A);A A2;
Write(A);
Read(B);B B2;
250
Write(B);
Read(B); B B+100; 50
Write(B);
150
250 150
A nonserializable schedule
Same as Schedule D
Schedule E but with new T2’
A B
T1 T2’ 25 25
Read(A); A A+100
Write(A);
125
Read(A);A A1;
Write(A);
Read(B);B B1;
125
Write(B);
Read(B); B B+100; 25
Write(B);
125
125 125
A schedule that is serializable only because of
the detailed behavior of the transactions.
• Consider schedules regardless of
– initial state and
– transaction semantics
• Only look at order of read and write
A Notation for Transactions and Schedules
Sc’=r1(A)w1(A) r1(B)w1(B)r2(A)w2(A)r2(B)w2(B)
T1 T2
Converting a conflict-serializable schedule to a
serial schedule by swaps of adjacent actions
T1 T2 T1 T2
no cycles Sc is “equivalent” to a
serial schedule
(in this case T1,T2)
For example, the following schedule S involves 3
transactions T1,T2 andT3.
S:r2(A);r1(B);w2(A);r3(A);w1(B);w3(A);r2(B);w2(B)
We can find :
T2<sT3
T1<sT2
The precedence graph for the schedule S is :
1 2 3
1 2 3
For example:
no lj(A)
S1 = l1(A)l1(B)r1(A)w1(B)l2(B)u1(A)u1(B)
r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)
Schedule F
T1 T2
l1(A);Read(A)
A A+100;Write(A);u1(A)
l2(A);Read(A)
A Ax2;Write(A);u2(A)
l2(B);Read(B)
B Bx2;Write(B);u2(B)
l1(B);Read(B)
B B+100;Write(B);u1(B)
Schedule F
A B
T1 T2 25 25
l1(A);Read(A)
A A+100;Write(A);u1(A) 125
l2(A);Read(A)
A Ax2;Write(A);u2(A) 250
l2(B);Read(B)
B Bx2;Write(B);u2(B) 50
l1(B);Read(B)
B B+100;Write(B);u1(B) 150
250 150
no unlocks no locks
Time
Growing Shrinking
Phase 1 Phase 2
Schedule G
T1 T2
l1(A);Read(A)
A A+100;Write(A)
l1(B); u1(A)
delayed
l2(A);Read(A)
A Ax2;Write(A);ll2(B)
Schedule G
T1 T2
l1(A);Read(A)
A A+100;Write(A)
l1(B); u1(A)
delayed
l2(A);Read(A)
A Ax2;Write(A);ll2(B)
Read(B);B B+100
Write(B); u1(B)
Schedule G
T1 T2
l1(A);Read(A)
A A+100;Write(A)
l1(B); u1(A)
delayed
l2(A);Read(A)
A Ax2;Write(A);ll2(B)
Read(B);B B+100
Write(B); u1(B)
l2(B); u2(A);Read(B)
B Bx2;Write(B);u2(B);
So far:
S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) …
Exclusive Locks
Transactions that read and write the same objects .
Ti = ...l-X1(A) … r1(A) ... w1(A) ... u(A) …
Three Requirements of a
Shared/Exclusive Lock System
1. Consistency of transactions
2. Two-phase locking of transactions
3. Legality of schedules
Consistency of Transactions
S = ....l-Si(A) … … ui(A) …
no l-Xj(A)
no l-Xj(A)
no l-Sj(A)
For example :Let us examine a possible schedule
of the following two transactions,using shared and
exclusive locks:
T1:sl1(A);r1(A);xl1(B);r1(B);w1(B);u1(A);u1(B)
T2:sl2(A);r2(A);sl2(B);r2(B);u2(A);u2(B)
T1 T2
sl1(A);r1(A);
sl2(A);r2(A); Notice that the sc
hedule is conflict-
sl2(B);r2(B); serializable, the c
xl1(B) Denied onflict-equivalent
serial order is (T2,
u2(A);u2(B) T1)
xl1(B);r1(B);w1(B);
u1(A);u1(B);
The compatibility matrix for shared and
exclusive locks
Lock requested
S X
Think of
- Get 2nd lock on A, or
- Drop S, get X lock
Upgrading locks allows more concurrent operation
T1:sl1(A);r1(A);sl1(B);r1(B);xl1(B);w1(B);u1(A);u1(B);
T2:sl2(A);r2(A);sl2(B);r2(B);u2(A);u2(B);
T1 T2
sl1(A);r1(A);
sl2(A);r2(A);
sl2(B);r2(B);
sl1(B);r1(B);
xl1(B) Denied
u2(A);u2(B)
xl1(B);w1(B)
u1(A);u2(B)
Indiscriminate use of upgrading introduces a new source
of potentially serious source of deadlocks
T1 T2
sl1(A)
sl2(A)
Xl1(A) Denied
xl2(A)Denied
Solution
If Ti wants to read A and knows it
may later want to write A, it requests
update lock (not shared)
Update Locks
S X U
S Yes No Yes
X No No No
U No No No
The update locks can fix the deadlock problem.
T1:ul1(A);r1(A);xl1(A);w1(A);u1(A);
T2:ul2(A);r2(A);xl2(A);w2(A);u2(A);
T1 T2
ul1(A);r1(A);
ul2(A)Denied
xl1(A);w1(A);u1(A);
ul2(A);r2(A);
xl2(A);w2(A);u2(A)
Increment Locks
• Atomic increment action: INi(A)
{Read(A); A A+k; Write(A)}
• INi(A), INj(A) do not conflict!
INi(A) A=7 INj(A)
+2 +10
A=5 +10
A=17
+2
INj(A)
A=15
INi(A)
A consistent transaction can only have increm
ent action on X if it holds an increment lock o
n X at the time. An increment lock does not e
nable either read or write actions
In a legal schedule,any number of transaction
s can hold an increment lock on X at any time
The action inci(X) conflicts with both rj(X) and
wj(X), but does not conflict with incj(X)
Compatibility matrix for
shared,exclusive,and increment locks
S X I
S Yes No No
X No No No
I No No .Yes
T1:sl1(A);r1(A);il1(B);inc1(B);u1(A);u1(B)
T2:sl2(A);r2(A);il2(B);inc2(B);u2(A);u2(B)
T1 T2
sl1(A);r1(A);
sl2(A);r2(A);
il2(B);inc2(B);
il1(B);inc1(B);
u2(A);u2(B);
u1(A);u1(B)
S: r1(A);r2(A);inc2(B);inc1(B)
r1(A);r2(A); inc1(B);inc2(B)
r1(A); inc1(B); r2(A); inc2(B)
Locking System
Ti
Read(A),Write(B)
Scheduler, part I
lock
table
Scheduler, part II
l(A),Read(A),l(B),Write(B)…
DB
Read(A),Write(B)
Lock table Conceptually
If null, object is unlocked
A
Every possible object
A
A... Lock info for A
H
...
T3 X yes
To other T3
records
The group mode is a summary of the most stringent
conditions that a transaction requesting a new lock on A.
Handling Lock Requests
• If there is no lock-table entry for A, the
entry is created and the request is granted
• If the lock-table entry for A exists
– If the group mode is “update”or
“exclusive”,then the request is denied
– If the group mode is “shared”,then another
shared or update lock can be granted,and we
need to modify the group mode
Handling Unlocks
• Delete T’s entry on the list for A
• Modify the group mode
– If the lock held by T is not the same as the group
mode,then we need not change the group mode
– Else we may have to examine the entire list to find the
new group mode
• If the value of waiting is “yes”, then we need to
grant one or more locks from the list of
requested locks
– First-come –first –served
– Priority to shared locks
– Priority to upgrading
What are the objects we lock?
...
B
DB DB ...
DB
Locking works in any case, but should
we choose small or large objects?
Blocks B1 B2 B3
t1 t2 t3 Tuples
Stall 1 Stall 2 Stall 3 Stall 4
restroom
hall
Warning Locks
IS IX S X
R E# Name ….
o1 55 Smith
o2 75 Jones
T1: Insert <99,Gore,…> into R
T2: Insert <99,Bush,…> into R
T1 T2
S1(o1) S2(o1)
S1(o2) S2(o2)
Check Constraint Check Constraint
...
...
Insert o3[99,Gore,..]
Insert o4[99,Bush,..]
Solution
• Use multiple granularity tree
• Before insert of node Q,
lock parent(Q) in
X mode R1
t1
t2 t3
Back to example
T1: Insert<99,Gore> T2: Insert<99,Bush>
T1 T2
X1(R)
X2(R) delayed
Check constraint
Insert<99,Gore>
U(R)
X2(R)
Check constraint
Oops! e# = 99 already in R!
The Motivation for Tree-Based
Locking
B C
T1 lock T1 lock
D
E F
T1 lock
A
T1 lock
B C
T1 lock T1 lock
D
E F
Why does this work?
• Assume all Ti start at root; exclusive lock
• Ti Tj Ti locks root before Tj
Root
Q T i Tj
U writes X T writes X
T1 T2 T3 A B C
200 150 175 RT=0 RT=0 RT=0
WT=0 WT=0 WT=0
r1(B) RT=200
r2(A) RT=150
r3(C) RT=175
w1(B) WT=200
w1(A) WT=200
w2(C)
Abort
w3(A)
Multiversion Timestamps
T1 T2 T3 T4 A
150 200 175 225 RT=0
WT=0
r1(A) RT=150
w1(A) WT=150
r2(A) RT=200
w2(A) WT=200
r3(A)
Abort
r4(A) RT=225
Execution of transactions using multiversion concurrency control
T1 T2 T3 T4 A0 A150 A200
150 200 175 255
r1(A) read
W1(A) create
r2(A) read
w2(A) create
r3(A) read
r4(A) read
Timestamps and Locking
• In low-conflict situations, timestamp
performs better
• In high-conflict situations, locking performs
better.
Concurrency Control by
Validation
1 T reads X U writes X
T start
U start T validating
U validated
RS(T2)={B} RS(T3)={A,B}=
WS(T2)={B,D} WS(T3)={C}
T2 T3 T2 T3
start validated validated
start
time
RS(T2)={B} RS(T3)={A,B}=
WS(T2)={B,D} WS(T3)={C}
T2 T3 T2 T3
start validated validated
start
T2 T3
time
finish start
phase 3
2 T writes X
U writes X
U validated T validating
U finish
T cannot validate if it could then write something
ahead of an earlier transaction
Another thing validation must prevent:
RS(T2)={A} RS(T3)={A,B}
WS(T2)={D,E} WS(T3)={C,D}
T2 T3
validated validated
finish
T2 time
BAD: w3(D) w2(D)
allow
Another thing validation must prevent:
RS(T2)={A} RS(T3)={A,B}
WS(T2)={D,E} WS(T3)={C,D}
T2 T3
validated validated
finish finish
T2 T2 time
For validating a transaction T
• Compare RS(T) with WS(U) and check
that RS(T)WS(U)= for any U that did
not finish before T started,i.e.,if
FIN(U)>START(T)
• Compare WS(T) with WS(U) and check
that WS(T) WS(U)= for any U that did
not finish before T VALIDATED,i.e.,if
FIN(U)>VAL(T)
start
Example: validate
finish
U: RS(U)={B} W: RS(W)={A,D}
WS(U)={D} WS(W)={A,C}
T: RS(T)={A,B} V: RS(V)={B}
WS(T)={A,C} WS(V)={D,E}
Validation of U : it validates successfully
Validation of T: ,
RS(T) WS(U)={A,B} {D}=
WS(T) WS(U)={A,C} {D}=
Validation of V
RS(V) WS(T)={B} {A,C}=
WS(V) WS(T)={D,E} {A,C}=
RS(V) WS(U)={B} {D}=
Validation of W
RS(W) WS(T)={A,D} {A,C}= {A}
RS(W) WS(V)={A,D} {D,E}= {D}
WS(W) WS(V)={A,C} {D,E}=
W is rolled back
Comparison of Three Concurrency-
Control Mechanisms
• Storage utilization
– Locks:space in the lock table is proportional to the
number of database elements locked
– Timestamps: Store read- and write-times in a table
analogous to a lock table
– Validation:Space is used for timestamps and
read/write sets for each currently active transaction
• The ability to complete without delay
– Locking delays but avoids rollbacks, timestamps and
validation do not delay, but cause them to rollback
Summary
Have studied C.C. mechanisms used in
practice
- 2 PL
- Multiple granularity
- Tree (index) protocols
- Timestamp
- Validation