4 5 PDF
4 5 PDF
1
Transactions and Concurrency Control - Introduction
2
Banking Example
3
Figure 13.1
Operations of the Account interface
deposit(amount)
deposit amount in the account
withdraw(amount)
withdraw amount from the account
getBalance() -> amount
return the balance of the account
setBalance(amount)
set the balance of the account to amount
4
Simple Synchronization without Transactions
5
Enhancing Client Cooperation by Signaling
6
Transactions
7
Atomicity
8
Figure 13.2
A client’s banking transaction
Transaction T:
a.withdraw(100);
b.deposit(100);
9
Figure 13.3
Operations in Coordinator interface
abortTransaction(trans);
aborts the transaction.
10
Figure 13.4
Transaction life histories
If a transaction aborts for any reason (self abort or server abort), it must be guaranteed
that future transaction will not see the its effect either in the object or in their copies in
permanent storage.
11
Concurrency Control: the lost update problem
Transaction T : Transaction U:
balance = b.getBalance(); balance = b.getBalance();
b.setBalance(balance*1.1); b.setBalance(balance*1.1);
a.withdraw(balance/10) c.withdraw(balance/10)
balance = b.getBalance(); $200
balance = b.getBalance(); $200
b.setBalance(balance*1.1); $220
b.setBalance(balance*1.1); $220
a.withdraw(balance/10) $80
c.withdraw(balance/10) $280
a, b and c initially have bank account balance are: 100, 200, and 300. T transfers an
amount from a to b. U transfers an amount from c to b.
12
Concurrency Control: The inconsistent retrievals problem
Transaction V: Transaction W:
a.withdraw(100)
aBranch.branchTotal()
b.deposit(100)
a.withdraw(100); $100
total = a.getBalance() $100
total = total+b.getBalance() $300
total = total+c.getBalance()
b.deposit(100) $300
13
Serial equivalence
Transaction T: Transaction U:
balance = b.getBalance() balance = b.getBalance()
b.setBalance(balance*1.1) b.setBalance(balance*1.1)
a.withdraw(balance/10) c.withdraw(balance/10)
15
Conflicting Operations
16
Figure 13.9
Read and write operation conflict rules
17
Figure 13.13
Nested transactions
T : top-level transaction
T1 = openSubTransaction T2 = openSubTransaction
commit
T1 : T2 :
openSubTransaction openSubTransaction openSubTransaction
prov. commit abort
T11 : T12 : T21 :
openSubTransaction
prov. commit prov. commit prov. commit
T211 :
prov.commit
18
Locks
19
Figure 13.14
Transactions T and U with exclusive locks
Transaction T: Transaction U:
balance = b.getBalance() balance = b.getBalance()
b.setBalance(bal*1.1) b.setBalance(bal*1.1)
a.withdraw(bal/10) c.withdraw(bal/10)
Operations Locks Operations Locks
openTransaction
bal = b.getBalance() lock B
b.setBalance(bal*1.1) openTransaction
a.withdraw(bal/10) Lock A bal = b.getBalance() waits for T’s
lock on B
closeTransaction unlock A, B
lock B
b.setBalance(bal*1.1)
c.withdraw(bal/10) lock C
closeTransaction unlock B, C
20
Figure 13.15
Lock compatibility
21
Shared Lock and Exclusive lock
T1 (Transfer) T2 (Dividend)
22
Lock-based protocol -- example
Example of schedule with locks
1. S-lock(X)
No wait: S-locks
2. A1 <- Read(X)
1. S-lock(X)
3. Unlock(X)
2. A1 <- Read(X)
4. A1 <- A1 – k
T1 waits
5. X-lock(X)
3. Unlock(X)
T1 can go ahead
4. A1 <- A1* 1.01 T2 waits
5. X-lock(X)
5. X-lock(X)
T1 6. Write(X, A1) T2
7. Unlock(X)
T2 can go ahead
8. …. 5. X-lock(X) 23
Lock based protocols -- questions
24
Lock based protocol – need for a protocol
S-lock (X)
1. A1 <- Read(X)
Unlock(X)
2. A1 <- A1 – k
X-lock (X)
S-lock (X)
Unlock(X) 3. Write(X, A1) 1. A1 <- Read(X) Unlock(X)
2. A1 <- A1* 1.01
X-lock(X)
3. Write(X, A1) Unlock(X),
S-lock(Y)
4. A2 <- Read(Y) Unlock(Y)
5. A2 <- A2 * 1.01
S-lock (Y)
6. Write(Y, A2) X-lock (Y)
Unlock(Y)
4. A2 <- Read(Y)
Unlock(Y)
5. A2 <- A2 + k Not conflict serializable.
X-lock (Y)
Unlock(Y)6. Write(Y, A2)
X : 100 -> 50 -> 50.5; Y : 200 -> 202 -> 252; X+Y = 302.5 25
Two-phase locking -- motivation
X-lock(X)
Write(X, 100)
Unlock(X) S-lock(X)
Read(X)
……
T1 T2
In this case, there is contention from T1 to T2
To ensure serializability, we must ensure there is no conflict
from T2 back to T1
How?
26
Two-phase locking -- motivation
27
Two phase locking – definition
28
Two phase locking – Serializability
29
2-phase locking -- example
1. S-lock(X)
2. A1 <- Read(X)
3. A1 <- A1 – k
4. X-lock(X) T2 waits
1. S-lock(X)
5. Write(X, A1)
6. S-lock(Y)
7. A2 <- Read(Y)
8. A2 <- A2 + k
9. X-lock(Y)
10. Write(Y, A2)
11. Unlock(X)
1. S-lock(X)
Lock point for T1 2. A1 <- Read(X)
3. A1 <- A1* 1.01
4. X-lock(X)
5. Write(X, A1)
12. Unlock(Y) T2 waits
6. S-lock(Y)
6. S-lock(Y)
7. A2 <- Read(Y)
8. A2 <- A2 * 1.01 Lock point for T2
9. X-lock(Y)
T1 10.
11.
Write(Y, A2)
Unlock(Y)
T2
12. Unlock(X) 30
Recoverability from aborts
31
Figure 13.11
A dirty read when transaction T aborts
Transaction T: Transaction U:
a.getBalance() a.getBalance()
a.setBalance(balance + 10) a.setBalance(balance + 20)
Premature write: related to the interaction between write operations on the same
object belonging to different transactions.
a. If U aborts and then T commit, we got a to be correct 105.
Some systems restore value to “Before images” value for abort action, namely the
value before all the writes of a transaction. a is 100, which is the before image of T’s
write. 105 is the before image of U’s write.
b. Consider if U commits and then T aborts, we got wrong value of 100.
c. Similarly if T aborts then U aborts, we got 105, which is wrong and should be 100.
So to ensure correctness, write operations must be delayed until earlier transactions
that updated the same object have either committed or aborted.
33
Recover problem with 2PL
1. X-lock(X)
2. A1 <- Read(X)
3. A1 <- A1 * 10
4. Write(X, A1)
5. Unlock(X)
1. X-lock(X)
2. A2 <- Read(X)
3. A2 <- A2 + 1
4. Write(X, A2)
5. Unlock(X)
6. Commit
6. Abort!
35
Strict two-phase Locking Protocol
36
Figure 13.16
Use of locks in strict two-phase locking
39
Figure 13.19
Deadlock with write locks
Transaction T Transaction U
40
Figure 13.20
The wait-for graph for Figure 13.19
Waits for B
Held by
41
Figure 13.21
A cycle in a wait-for graph
42
Figure 13.22
Another wait-for graph
V T
Held by W
T Held by Held by
C
U
Held by B
U
W V
Waits for
43
Deadlock Prevention
Deadlock prevention:
ִSimple way is to lock all of the objects used by a
transaction when it starts. It should be done as an atomic
action to prevent deadlock. a. inefficient, say lock an
object you only need for short period of time. b. Hard to
predict what objects a transaction will require.
ִJudge if system can remain in a Safe state by satisfying a
certain resource request. Banker’s algorithm.
ִOrder the objects in certain order. Acquiring the locks
need to follow this certain order.
44
Safe State
47
Example
48
Request Example
49
Resource-Allocation Graph and Wait-for Graph
50
Deadlock Detection
Transaction T Transaction U
Operations Locks Operations Locks
52
Optimistic Concurrency Control
53
Optimistic Concurrency Control
54
Optimistic Concurrency Control
55
Optimistic Concurrency Control
56
Validation of Transactions
Tv Ti Rule
write read 1. Ti must not read objects written by Tv
read write 2. Tv must not read objects written by Ti
write write 3. Ti must not write objects written by Tv and
Tv must not write objects written by Ti
T1
Earlier committed
transactions
T2
T3
Transaction
being validated Tv
active
1
Later active
active
transactions 2
59
Validation
60
Page 547-548
Validation of Transactions
61
Timestamp based concurrency control
64
Figure 13.29
Operation conflicts for timestamp ordering
Rule Tc Ti
1. write read Tc must not write an object that has been read by any Ti where Ti >Tc
this requires that Tc ≥ the maximum read timestamp of the object.
2. write write Tc must not write an object that has been written by any Ti where Ti >Tc
this requires that Tc > write timestamp of the committed object.
3. read write Tc must not read an object that has been written by any Ti whereTi >Tc
this requires that Tc > write timestamp of the committed object.
T2 T1 T2 Key:
Before Before Ti
Committed
After T2 T3 After T1 T2 T3
Ti
Time Time
Tentative
object produced
(c) T3 write (d) T3 write by transaction Ti
Transaction (with write timestamp Ti)
T1 T4 T4 aborts T1<T2<T3<T4
Before Before
After T1 T3 T4 After T4
Time Time
66
Page 551
Timestamp ordering write rule
67
Page 551
Timestamp ordering read rule
68
Figure 13.31
Read operations and timestamps
(b) T3 read
(a) T3 read
Key:
read read
T2 T2 T4 proceeds
proceeds Ti
Selected Committed
Selected Time
Time
Ti
(c) T3 read (d) T3 read
Tentative
69
Exercise
T U A B C
RTS WTS RTS WTS RTS WTS
{} S {} S {} S
openTransaction
bal = b.getBalance()
openTransaction
b.setBalance(bal*1.1)
bal = b.getBalance()
a.withdraw(bal/10)
commit
bal = b.getBalance()
b.setBalance(bal*1.1)
c.withdraw(bal/10)
S<T<U 70
Figure 13.32
Timestamps in transactions T and U
T U A B C
RTS WTS RTS WTS RTS WTS
{} S {} S {} S
openTransaction
bal = b.getBalance() {T}
openTransaction
b.setBalance(bal*1.1) S, T
bal = b.getBalance()
wait for T
a.withdraw(bal/10) S, T
commit T T
bal = b.getBalance() {U}
b.setBalance(bal*1.1) T, U
c.withdraw(bal/10) S, U
S<T<U 71
Test Your Understanding
1. A server manages the objects a1, a2, ... an. The server provides two
operations for its clients:
read (i) returns the value of ai;
write(i, Value) assigns Value to ai.
The transactions T and U are defined as follows:
T: x = read(j); y = read (i); write(j, 44); write(i, 33);
U: x = read(k); write(i, 55); y = read (j); write(k, 66).
72
Test Your Understanding
2. Consider the use of timestamp ordering with each of the example
interleavings of transactions T and U in Exercise 13.9. Initial values of
ai and aj are 10 and 20, respectively, and initial read and write
timestamps are t0. Assume that each transaction opens and obtains a
timestamp just before its first operation; for example, in (a) T and U get
timestamps t1 and t2 respectively, where t0 < t1 < t2. Describe in order
of increasing time the effects of each operation of T and U. For each
operation, state the following:
73