Topic 3 Concurrency Control
Topic 3 Concurrency Control
Control
Bassam Hammo
Transactions
1
Transactions
Atomicity Consistency
Transactions are atomic – they Transactions take the database
don’t have parts (conceptually) from one consistent state into
can’t be executed partially; it another
should not be detectable that In the middle of a transaction
they interleave with another the database might not be
transaction consistent
2
Atomicity
Consistency
Ti
Consistent Database Consistent Database
3
Isolation and Durability
Isolation Durability
The effects of a transaction are Once a transaction has
not visible to other transactions completed, its changes are made
until it has completed permanent
From outside the transaction has Even if the system crashes, the
either happened or not effects of a transaction must
To me this actually sounds like a remain in place
consequence of atomicity…
Isolation
4
Global Recovery
Example of a transaction
5
The Transaction Manager
Concurrency
6
Concurrency Problems
Lost Update
7
Uncommitted Update
Inconsistent analysis
8
Need for concurrency control
Schedules
9
The Scheduler
Serial schedules
10
Serializability
Uses of Serializability
being serializable means
the schedule is equivalent to some serial schedule
Serial schedules are correct
Therefore, serializable schedules are also correct schedules
serializability is hard to test
Use precedence graph (PG)
Need the methods (or protocols) to enforce serializabilty
Two phase locking(2PL)
Time stamp ordering (TSO)
11
Conflict Serialisability
Conflicting Operations
12
Precedence Graph (PG)
Precedence graph
Used to test for conflict serializability of a schedule
A directed graph G=(V,E)
V: a finite set of transactions
E: a set of arcs from Ti to Tj if an action of Ti comes first and conflicts
with one of Tj’s actions
More on PG
13
Serialization Graph
Consider the schedule S:
Time T1 T2
t1 Write(X)
t2 Read(Y)
t3 Read(Y)
t4 Read(X)
T1 T2
Serialization Graph
Consider the schedule:
Time T1 T2 T3
t1 Read (X)
t2 Write (Y)
t3 Write (X)
t4 Read (X)
t5 Read (Y)
T1 T2
There is a cycle. T3
Hence it is NOT conflict serializable
14
Serialization Graph
Consider the schedule:
Time T1 T2
t1 read(balx)
t2 read(balx)
t3 write(balx)
t4 read(baly)
t5 write(baly)
t6 read(baly)
t7 write(baly)
T1 T2
There is a cycle.
Hence it is NOT conflict serializable
Serialization Graph
Consider the following PG:
T1 T2
T3
15
Serialization Graph
Consider the following PG:
T1 T2
T3
Cycle T1 T2 T1
Cycle T1 T2 T3 T1
16
Locking
Locking
Lock Table
Serializable Schedule
17
Two-Phase Locking Protocol
2 PL Protocol
Basics of locking:
Each transaction T must obtain a S ( shared) lock on object before reading,
and an X ( exclusive) lock on object before writing.
If an X lock is granted on object O, no other lock (X or S) might be
granted on O at the same time.
If an S lock is granted on object O, no X lock might be granted on O at the
same time.
Conflicting locks are expressed by the compatibility matrix:
S X
S √ --
X -- --
18
Basics of Locking
Time T1 T2
t1 start
t2 start lock-X(balx)
t3 lock-X(balx) read(balx)
t4 wait balx=balx + 100
t5 wait write(balx)
t6 wait commit/unlock(balx)
t7 read(balx)
t8 balx=balx -10
t9 write(balx)
t10 commit/unlock(balx)
19
Preventing Uncommitted Dependency Problem using 2PL
Time T1 T2
t1 start
t2 lock-X(balx)
t3 read(balx)
t4 start balx=balx + 100
t5 lock-X(balx) write(balx)
t6 wait rollback/unlock(balx)
t7 read(balx)
t8 balx=balx -10
t9 write(balx)
t10 commit/unlock(balx)
20
Locking methods: problems
Deadlock
Time T1 T2
t1 lock-S(A)
t2 lock-S(B)
t3 read(B)
t4 read(A)
t5 lock-X(B)
t6 lock-X(A)
21
Deadlock Example
Time T1 T2
t1 start
t2 lock-X(balx) start
t3 read(balx) lock-X(baly)
t4 balx=balx -10 read(baly)
t5 write (balx) baly=baly + 100
t6 lock-X(baly) write (baly)
t7 wait lock-X(balx)
t8 wait wait
t9 wait wait
t10 .. ..
Deadlock Detection
22
Precedence/Wait-For Graphs
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)
T1
T3 Read(X)
T1 Write(Y)
T2 T3
Precedence graph
23
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)
T1
T3 Read(X)
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)
T1
T3 Read(X)
T1 Write(Y)
T2 T3
Precedence graph
24
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)
T1
T3 Read(X)
T1 Write(Y)
T2 T3
Precedence graph
Example
T1 Read(X) S-lock(X) T1
T2 Read(Y) S-lock(Y)
T1 Write(X) X-lock(X)
T2 T3
T2 Read(X) tries S-lock(X)
T3 Read(Z) Wait for graph
T3 Write(Z)
T1 Read(Y)
T1
T3 Read(X)
T1 Write(Y)
T2 T3
Precedence graph
25
Example
T1 Read(X) S-lock(X) T1
T2 Read(Y) S-lock(Y)
T1 Write(X) X-lock(X)
T2 T3
T2 Read(X) tries S-lock(X)
T3 Read(Z) S-lock(Z) Wait for graph
T3 Write(Z) X-lock(Z)
T1 Read(Y) S-lock(Y)
T1
T3 Read(X) tries S-lock(X)
T1 Write(Y)
T2 T3
Precedence graph
Example
T1 Read(X) S-lock(X) T1
T2 Read(Y) S-lock(Y)
T1 Write(X) X-lock(X)
T2 T3
T2 Read(X) tries S-lock(X)
T3 Read(Z) S-lock(Z) Wait for graph
T3 Write(Z) X-lock(Z)
T1 Read(Y) S-lock(Y)
T1
T3 Read(X) tries S-lock(X)
T1 Write(Y) tries X-lock(Y)
T2 T3
Precedence graph
26
Solution
Deadlock Prevention
27
Deadlock Prevention
28
Timestamp
Timestamp
29
Timestamp Protocol
Timestamp Example 1
30
Timestamp Example 1
X Y Z
R
T1 T2
Read(X) Read(X) W
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS
Timestamp Example 1
X Y Z
R 1
T1 T2
Read(X) Read(X) W
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 1
31
Timestamp Example 1
X Y Z
R 2
T1 T2
Read(X) Read(X) W
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 1 2
Timestamp Example 1
X Y Z
R 2 1
T1 T2
Read(X) Read(X) W
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 1 2
32
Timestamp Example 1
X Y Z
R 2 2
T1 T2
Read(X) Read(X) W
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 1 2
Timestamp Example 1
X Y Z
R 2 2
T1 T2
Read(X) Read(X) W
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 1 2
33
Timestamp Example 1
X Y Z
R 2 2
T1 T2
Read(X) Read(X) W
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 1 2
Timestamp Example 1
X Y Z
R 2 2
T1 T2
Read(X) Read(X) W
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 1 2
34
Timestamp Example 1
X Y Z
R 2 2
T1 T2
Read(X) Read(X) W
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 3 2
Timestamp Example 1
X Y Z
R 2 2
T1 T2
Read(X) Read(X) W 2
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 3 2
35
Timestamp Example 1
X Y Z
R 3 2
T1 T2
Read(X) Read(X) W 2
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 3 2
Timestamp Example 1
X Y Z
R 3 3
T1 T2
Read(X) Read(X) W 2
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 3 2
36
Timestamp Example 1
X Y Z
R 3 3
T1 T2
Read(X) Read(X) W 2
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 3 2
Timestamp Example 1
X Y Z
R 3 3
T1 T2
Read(X) Read(X) W 3 2
Read(Y) Read(Y)
Y=Y+X Z=Y-X
T1 T2
Write(Y) Write(Z)
TS 3 2
37
Timestamp ordering – example 2
Consider the following concurrent schedule
38
Thomas’ write rule
Timestamp
39
Optimistic concurrency control
40