Concurrency
Concurrency
Database I
CSIS2300
Faculty of Business Administration
Concurrency Control
Concurrency Control Techniques
• Objectives are…
▪ Understand transactions.
▪ Understand the need for concurrency control.
▪ Understand serial and serializable schedules.
▪ Understand the need for locks and locking protocols.
3
Introduction
4
Introduction (cont’d)
5
Introduction (cont’d)
6
Schedules
7
Serializability of Schedules
• Schedules that are considered correct when concurrent
transactions are executing.
8
Two Sample Transactions
T1 T2
read_item(X); read_item(X);
X:=X-N;
X:=X+M;
write_item(X);
write_item(X);
read_item(Y);
Y:=Y+N;
write_item(Y);
9
No Interleaving Schedule A
Time
T1
read_item(X);
X:=X-N;
write_item(X);
read_item(Y);
Y:=Y+N;
write_item(Y);
T2
read_item(X);
X:=X+M;
write_item(X);
10
No Interleaving Schedule B
Time
T2
read_item(X);
X:=X+M;
T1 write_item(X);
read_item(X);
X:=X-N;
write_item(X);
read_item(Y);
Y:=Y+N;
write_item(Y);
11
Serial Schedules
12
Schedule B Example
Time
T2
read_item(X=90);
X:=(X=90)+(M=2);
T1 write_item(X=92);
read_item(X=92);
X:=(X=92)-(N=3);
write_item(X=89);
read_item(Y=90);
Y:=(Y=90)+(N=3);
write_item(Y=93);
13
Serial Schedules
14
Serial Schedules
15
Serial Schedules
Disadvantages...
▪ Must wait (long time) for other transactions to end;
▪ Prevents CPU from time sharing while waiting (for I/O
e.g.);
▪ Therefore generally NOT acceptable in practice.
16
Serial Schedules
17
Interleaving Schedule C
Time
T1 T2
read_item(X);
X:=X-N;
read_item(X);
X:=X+M;
write_item(X);
read_item(Y);
write_item(X);
Y:=Y+N;
write_item(Y);
18
Interleaving Schedules
19
Interleaving Schedule C
Time
T1 T2
read_item(X=90);
X:=(X=90)-(N=3);
read_item(X=90);
X:=(X=90)+(M=2);
write_item(X=87);
read_item(Y=90);
write_item(X=92);
Y:=(Y=90)+(N=3);
write_item(Y=93);
20
Interleaving Schedule D
Time
T1 T2
read_item(X);
X:=X-N;
write_item(X);
read_item(X);
X:=X+M;
write_item(X);
read_item(Y);
Y:=Y+N;
write_item(Y);
21
Interleaving Schedules
22
Interleaving Schedule D
Time
T1 T2
read_item(X=90);
X:=(X=90)-(N=3);
write_item(X=87);
read_item(X=87);
X:=(X=87)+(M=2);
write_item(X=89);
read_item(Y=90);
Y:=(Y=90)+(N=3);
write_item(Y=93);
23
Serializability
24
Serializability
25
Precedence Graphs
T1 T2
Schedule A
T2 T1
Schedule B
27
Precedence Graphs: an Example
Time
T3
read_item(A);
A := A-50; T4
read_item(A);
temp := A * 0.1;
A := A - temp
write_item(A);
read_item(B)
write_item(A);
read_item(B); See Slide 26 for conditions.
B := B + 50; Red arrow= Condition 2
write_item(B); Green arrow= Condition 3
Condition 1 not shown
B := B + temp
write_item(B);
28
Precedence Graphs
T3 T4
29
Precedence Graphs
More examples
T1 T2 PG has a
cycle so
not
serializable
T4 T3
30
Precedence Graphs
More examples
T1 T2 PG is
serializable
T4 T3
31
Precedence Graphs
Topological Sorting
T1 T2 Pick a
node with
NO edges
going into
it, left
with…
T4 T3
32
Precedence Graphs
Topological Sorting
T1 T2 Pick a
node with
NO edges
going into
it, left
with…
T4
33
Precedence Graphs
Topological Sorting
T2
Therefore
serializability order is :
T3→T1 → T2 →T4.
It is possible to have
T4 more than one
serializability order.
34
Transaction Support in SQL
• A single SQL statement is always considered atomic
– either it completes execution without error or it fails.
• ANSI SQL has no explicit Begin_Transaction
statement.
• But every transaction must have Commit or Rollback.
35
Concurrency Control Techniques
• Techniques used to ensure noninterference or
isolation property of concurrently executing
transactions.
• Most techniques ensure serializability of schedules
using protocols.
• Locking protocols are used in most commercial
DBMSs.
• Also Timestamping protocol, et al.
36
Locking Protocol
37
Manual Locking
38
Manual Locking (cont’d)
• Syntax is:
SELECT columnnames
FROM tablenames
WHERE condition
FOR UPDATE OF columnnames
[NOWAIT]
39
Binary Locks (Auto)
40
Binary Locks (cont’d)
41
Binary Locks (cont’d)
42
Binary Locks: an Example (pseudo-code)
T1 T2
read_lock(X);
read_lock(Y);
read_item(X);
read_item(Y); unlock(X);
unlock(Y); write_lock(Y);
write_lock(X); read_item(Y);
Y:=X+Y;
read_item(X);
write_item(Y);
X:=X+Y; unlock(Y);
write_item(X);
unlock(X);
43
Binary Locks
44
Shared/Exclusive Locks (Auto)
45
Non-serializable Schedule
Time
T1
T2
read_lock(Y=30);
read_item(Y);
unlock(Y); read_lock(X=20);
read_item(X);
unlock(X);
write_lock(Y=30);
read_item(Y);
Y:=(X=20)+(Y=30);
write_item(Y=50);
unlock(Y); cont’d next page
46
Non-serializable Schedule
from prev page Time
T1 T2
write_lock(X=20);
read_item(X);
X:=(X=20)+(Y=30);
write_item(X=50);
unlock(X);
47
Two-Phase Locking (Auto)
• ALL locking operations (read, write) precede the first
unlock operation
• Growing Phase
▪ New locks acquired, none released
• Shrinking Phase
▪ Acquired locks released, no new locks can be acquired.
48
Two-Phase Locking: Rewritten
T1 T2
read_lock(X);
read_lock(Y);
read_item(X);
read_item(Y); write_lock(Y);
write_lock(X); unlock(X)
read_item(Y);
unlock(Y)
Y:=X+Y;
read_item(X);
write_item(Y);
X:=X+Y; unlock(Y);
write_item(X);
unlock(X);
49
Two-Phase Locking
50
Deadlocks
51
Deadlock Example
T1 Time
lock-X(B); T2 issues request. Waiting
read(B) for T1 to release.
B := B - 50; T2
write(B);
lock-S(A);
read(A)
lock-S(B);
lock-X(A); Requests for locks pending.
T1 issues request. Waiting Transactions deadlocked.
for T2 to release.
52
Deadlocks
53
Timestamping
54
Timestamping (cont’d)
55
Timestamping (cont’d)
56
Timestamping (cont’d)
57
Timestamping (cont’d)
59
Timestamping (cont’d)
60
Timestamping (cont’d)
61
What Do You Care About?
• When You Read?
▪ When was it last written to?
• When You Write?
▪ When was it last written to?
• Someone wrote to it after I started
▪ When was it last read?
• Somebody else is holding a copy of it
62
Why MaxOf(Read)?
Ti00:00 read(X)
63
Why MaxOf(Read)? Cont’d
• If set to -00:00
▪ You lied. It was read at 00:07
• If set to 00:07
▪ You missed the point
▪ We are trying to order transactions, not individual items
(or operations)
▪ Consider the cigarettes in the supermarket example
▪ If two customers at the checkout want to buy the last
carton of cigarettes, who should get it? The one who
noticed there was only one carton left and shouts “its
mine!” (real time) or the one at the head of the lineup?
• If set to 00:00 then it’s correct because it is more
recent than -00:00.
64
Timestamping
65
Feasible TS Schedule
66
Infeasible TS Schedule
67
Infeasible TS Schedule
read_item(A
r00:01
w00:00);
T15 00:02
T14 > Aw T14 >= Ar Set Br to Max
write_item(A r00:01);
w00:01 read_item(B r00:02);
w00:00
T15 >= Bw
Set Aw to T14 write_item(B r00:02 );
w00:02 Set Bw to T15
r00:02
read_item(B w00:02 );
T14 < Bw
write_item(B);
68
INFeasible TS Schedule
T14 00:01
read_item(A $20.00 ); T15 00:02
write_item(A$20 - $20);
$0
read_item(B $8.00 );
write_item(B $0 );
read_item(B $0 );
write_item(B A$20 + B$0 );
69
Time Stamping (last word)
70