Download presentation
Presentation is loading. Please wait.
1
Notes 09: Transaction Processing
Slides are modified from the CS 245 class slides of Hector Garcia-Molina
2
MySQL and Transactions
3.3.6 SET TRANSACTION Syntax, Transaction Isolation Levels,
3
Reading Chapter 10.1, 10.2 Chapter 11.1 – 11.3, 11.6
4
Transactions A sequence of operations on one or more data items.
Fundamental assumption: a transaction when run on a consistent state and without interference with other transactions will leave the database in a consistent state.
5
Termination of Transactions
Commit: guarantee that all of the changes of the transaction are permanent Abort: guarantee that none of the effects of the transaction are permanent Reason for abort: user, transaction, system failure
6
Chapter 9 Concurrency Control
T1 T2 … Tn DB (consistency constraints)
7
Example: T1: Read(A) T2: Read(A) A A+100 A A2 Write(A) Write(A)
Read(B) Read(B) B B+100 B B2 Write(B) Write(B) Constraint: A=B
8
Schedule A A B 25 25 T1 T2 Read(A); A A+100 125 Write(A);
25 25 125 250 T1 T2 Read(A); A A+100 Write(A); Read(B); B B+100; Write(B); Read(A);A A2; Read(B);B B2;
9
Schedule B A B 25 25 T1 T2 Read(A);A A2; 50 Write(A);
25 25 50 150 T1 T2 Read(A);A A2; Write(A); Read(B);B B2; Write(B); Read(A); A A+100 Read(B); B B+100;
10
Schedule C A B 25 25 T1 T2 Read(A); A A+100 125 Write(A);
25 25 125 250 T1 T2 Read(A); A A+100 Write(A); Read(A);A A2; Read(B); B B+100; Write(B); Read(B);B B2;
11
Schedule D A B 25 25 T1 T2 Read(A); A A+100 125 Write(A);
25 25 125 250 50 150 T1 T2 Read(A); A A+100 Write(A); Read(A);A A2; Read(B);B B2; Write(B); Read(B); B B+100;
12
Schedule E A B 25 25 T1 T2’ Read(A); A A+100 125 Write(A);
Same as Schedule D but with new T2’ A B 25 25 125 25 T1 T2’ Read(A); A A+100 Write(A); Read(A);A A1; Read(B);B B1; Write(B); Read(B); B B+100;
13
Want schedules that are “good”, regardless of
initial state and transaction semantics Only look at order of read and writes Example: Sc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)
14
Example: Sc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B) Sc’=r1(A)w1(A) r1(B)w1(B)r2(A)w2(A)r2(B)w2(B) T T2
15
However, for Sd: Sd=r1(A)w1(A)r2(A)w2(A) r2(B)w2(B)r1(B)w1(B) as a matter of fact, T2 must precede T1 in any equivalent schedule, i.e., T2 T1
16
T1 T2 Sd cannot be rearranged into a serial schedule
Also, T1 T2 T1 T2 Sd cannot be rearranged into a serial schedule Sd is not “equivalent” to any serial schedule Sd is “bad”
17
Returning to Sc Sc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)
T1 T2 T1 T2 no cycles Sc is “equivalent” to a serial schedule (in this case T1,T2)
18
Concepts Transaction: sequence of ri(x), wi(x) actions
Conflicting actions: r1(A) w2(A) w1(A) w2(A) r1(A) w2(A) Schedule: represents chronological order in which actions are executed Serial schedule: no interleaving of actions or transactions
19
Transaction A transaction T is a partial order, denoted by <, where
T { r[x], w[x]:x is a data item} {a, c} aT iff cT Suppose t is either a or c, than for any other operation pT , p<t
20
Complete History A complete history H over T={T1, …, Tn} is a partial order , <H, where H= (i=1, …, n) Ti <H (i=1, …, n) <I For any pair of conflicting operations p, q, either p <H q or q<Hp
21
Conflict equivalent histories
H1, H2 are conflict equivalent if Both are over the same transactions and have the same operations, and They order the conflicting operations the same way, i.e., if H1 can be transformed into H2 by a series of swaps on non-conflicting actions.
22
Conflict Serializable History
A history is conflict serializable if it is conflict equivalent to some serial history.
23
View Equivalence Let T1: x=x+1, T2: x=x*3 Let initially x=5
Serial histories: T1, T2, final value x=18 T2, T1, final value x=16 No efficient algorithm to guarantee view serializability
24
Precedence graph P(S) (S is schedule/ history)
Nodes: transactions in S Arcs: Ti Tj whenever - pi(A), qj(A) are actions in S - pi(A) <S qj(A) - at least one of pi, qj is a write
25
Exercise: What is P(S) for S = w3(A) w2(C) r1(A) w1(B) r1(C) w2(A) r4(A) w4(D) Is S serializable?
26
Lemma S1, S2 conflict equivalent P(S1)=P(S2) Proof:
Assume P(S1) P(S2) Ti: Ti Tj in S1 and not in S2 S1 = …pi(A)... qj(A)… pi, qj S2 = …qj(A)…pi(A)... conflict S1, S2 not conflict equivalent
27
Note: P(S1)=P(S2) S1, S2 conflict equivalent
Counter example: S1=w1(A) r2(A) w2(B) r1(B) S2=r2(A) w1(A) r1(B) w2(B)
28
Theorem P(S1) acyclic S1 conflict serializable
() Assume S1 is conflict serializable Ss: Ss, S1 conflict equivalent P(Ss) = P(S1) P(S1) acyclic since P(Ss) is acyclic
29
Theorem P(S1) acyclic S1 conflict serializable T1 T2 T3
() Assume P(S1) is acyclic Transform S1 as follows: (1) Take T1 to be transaction with no incident arcs (2) Move all T1 actions to the front S1 = ……. qj(A)…….p1(A)….. (3) we now have S1 = < T1 actions ><... rest ...> (4) repeat above steps to serialize rest!
30
How to enforce serializable schedules?
Option 1: run system, recording P(S); at end of day, check for P(S) cycles and declare if execution was good
31
How to enforce serializable schedules?
Option 2: prevent P(S) cycles from occurring T1 T2 ….. Tn Scheduler DB
32
What to do with aborted transactions?
If a transaction T aborts, the DBMS must: Undo all values written by T Abort all transactions that read any value written by T (avoid dirty read) Ensure recoverability Strict execution: a transaction can read or write only committed values.
33
A locking protocol Two new actions: lock (exclusive): li (A)
unlock: ui (A) T1 T2 lock table scheduler
34
Rule #1: Well-formed transactions
Ti: … li(A) … pi(A) … ui(A) ... That is, if the transaction 1) locks an item in an appropriate mode before accessing it and 2) unlocks each item that it locks.
35
Rule #2 Legal scheduler S = …….. li(A) ………... ui(A) ……...
If the transaction does not lock any item if it is already locked in a conflicting mode. no lj(A)
36
Exercise: What schedules are legal? What transactions are well-formed?
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) S2 = l1(A)r1(A)w1(B)u1(A)u1(B) l2(B)r2(B)w2(B)l3(B)r3(B)u3(B) S3 = l1(A)r1(A)u1(A)l1(B)w1(B)u1(B) l2(B)r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)
37
Exercise: What schedules are legal? What transactions are well-formed?
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) S2 = l1(A)r1(A)w1(B)u1(A)u1(B) l2(B)r2(B)w2(B)l3(B)r3(B)u3(B) S3 = l1(A)r1(A)u1(A)l1(B)w1(B)u1(B) l2(B)r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)
38
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)
39
Schedule F A B T1 T2 25 25 l1(A);Read(A) A A+100;Write(A);u1(A) 125
A Ax2;Write(A);u2(A) 250 l2(B);Read(B) B Bx2;Write(B);u2(B) l1(B);Read(B) B B+100;Write(B);u1(B)
40
Rule #3 Two phase locking (2PL) for transactions
Ti = ……. li(A) ………... ui(A) ……... no unlocks no locks
41
# locks held by Ti Time Growing Shrinking Phase Phase
42
Schedule G T1 T2 l1(A);Read(A) A A+100;Write(A) l1(B); u1(A)
A Ax2;Write(A);l2(B) delayed
43
Schedule G T1 T2 l1(A);Read(A) A A+100;Write(A) l1(B); u1(A)
A Ax2;Write(A);l2(B) Read(B);B B+100 Write(B); u1(B) delayed
44
Schedule G T1 T2 l1(A);Read(A) A A+100;Write(A) l1(B); u1(A)
A Ax2;Write(A);l2(B) Read(B);B B+100 Write(B); u1(B) l2(B); u2(A);Read(B) B Bx2;Write(B);u2(B); delayed
45
Schedule H (T2 reversed)
T1 T2 l1(A); Read(A) l2(B);Read(B) A A+100;Write(A) B Bx2;Write(B) l1(B) l2(A) delayed delayed
46
Assume deadlocked transactions are rolled back
They have no effect They do not appear in schedule E.g., Schedule H = This space intentionally left blank!
47
Next step: Show that rules #1,2,3 conflict- serializable schedules
Advantage: Easy to guarantee these properties!
48
Conflict rules for li(A), ui(A): li(A), lj(A) conflict
li(A), uj(A) conflict Note: no conflict < ui(A), uj(A)>, < li(A), rj(A)>,...
49
Theorem Rules #1,2,3 conflict
(2PL) serializable schedule To help in proof: Definition Shrink(Ti) = SH(Ti) = first unlock action of Ti
50
Lemma Ti Tj in S SH(Ti) <S SH(Tj) Proof of lemma: Ti Tj means that S = … pi(A) … qj(A) …; p,q conflict By rules 1,2: S = … pi(A) … ui(A) … lj(A) ... qj(A) … By rule 3: SH(Ti) SH(Tj) So, SH(Ti) <S SH(Tj)
51
Theorem Rules #1,2,3 conflict (2PL) serializable schedule
Proof: (1) Assume P(S) has cycle T1 T2 …. Tn T1 (2) By lemma: SH(T1) < SH(T2) < ... < SH(T1) (3) Impossible, so P(S) acyclic (4) S is conflict serializable
52
Problem with 2PL Deadlock Deadlock detection, prevention, avoidance
53
Variations of 2PL Conservative 2PL:each transaction must acquire all its locks before any of the operations are submitted Adv: no deadlocks Disadv: need read and write sets in advance Starvation
54
Variations of 2PL Strict 2PL: transactions release their locks after either commit or abort Adv: transaction has all the locks needed, recoverable histories, avoid cascading aborts Disadv: cannot avoid deadlocks, performance tradeoffs
55
Deadlock Avoidance 1. Order database object in linear order and transactions must acquire locks in this order Adv.: avoid deadlock Disadv.: need order in advance and need to know all the operations in advance
56
Deadlock Avoidance 2. Time-out: if a transaction waits for a lock longer than a predefined time period then the transaction aborts Adv.: no information about the transactions is needed in advance Disadv.: potentially cascading aborts
57
Deadlock Avoidance 3. Wait-for-graph: keep a record on which transactions waits for which transaction to release a lock. If cycle then there is a deadlock. Adv.: simplified administration Disadv.: which transaction to abort?
58
Beyond the variations of 2PL protocol, it is all a matter of improving performance and allowing more concurrency…. Shared locks Multiple granularity (database -> cell) Inserts, deletes and phantoms Other types of C.C. mechanisms
59
Shared locks So far: S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) …
Do not conflict Instead: S=... ls1(A) r1(A) ls2(A) r2(A) …. us1(A) us2(A)
60
Lock actions l-ti(A): lock A in t mode (t is S or X) u-ti(A): unlock t mode (t is S or X) Shorthand: ui(A): unlock whatever modes Ti has locked A
61
Rule #1 Well formed transactions
Ti =... l-S1(A) … r1(A) … u1 (A) … Ti =... l-X1(A) … w1(A) … u1 (A) …
62
What about transactions that read and write same object?
Option 1: Request exclusive lock Ti = ...l-X1(A) … r1(A) ... w1(A) ... u(A) …
63
Option 2: Upgrade What about transactions that read and
write same object? Option 2: Upgrade (E.g., need to read, but don’t know if will write…) Ti=... l-S1(A) … r1(A) ... l-X1(A) …w1(A) ...u(A)… Think of - Get 2nd lock on A, or - Drop S, get X lock
64
Rule #2 Legal scheduler S = ....l-Si(A) … … ui(A) …
no l-Xj(A) (but can have l-Sj(A) ) S = ... l-Xi(A) … … ui(A) … no l-Xj(A) no l-Sj(A)
65
A way to summarize Rule #2
Compatibility matrix Comp S X S true false X false false
66
Rule # 3 2PL transactions No change except for upgrades:
(I) If upgrade gets more locks (e.g., S {S, X}) then no change! (II) If upgrade releases read (shared) lock (e.g., S X) - can be allowed in growing phase
67
Proof: similar to X locks case
Theorem Rules 1,2,3 Conf.serializable for S/X locks schedules Proof: similar to X locks case Detail: l-ti(A), l-rj(A) do not conflict if comp(t,r) l-ti(A), u-rj(A) do not conflict if comp(t,r)
68
Lock types beyond S/X Examples: (1) increment/decrement lock
(2) update lock
69
Example (1): increment lock
Atomic increment action: INi(A) {Read(A); A A+k; Write(A)} INi(A), INj(A) do not conflict! A=7 A= A=17 A=15 INj(A) +10 INi(A) +2 +10 INj(A) +2 INi(A)
70
Comp S X I S X I
71
How about adding another column and row for decrement operations?
Comp S X I S T F F X F F F I F F T How about adding another column and row for decrement operations?
72
Update locks A common deadlock problem with upgrades: T1 T2 l-S1(A)
l-X1(A) l-X2(A) --- Deadlock ---
73
Solution If Ti wants to read A and knows it
may later want to write A, it requests update lock (not shared)
74
New request Comp S X U S X U Lock already held in
75
New request Comp S X U S T F T X F F F U TorF F F
-> symmetric table? Lock already held in
76
Note: object A may be locked in different modes at the same time...
S1=...l-S1(A)…l-S2(A)…l-U3(A)… l-S4(A)…? l-U4(A)…? To grant a lock in mode t, mode t must be compatible with all currently held locks on object
77
Database hierarchy (tree)
Locking Granularity Database Relation Attributes Tuple Database hierarchy (tree)
78
Locking Granularity Tree: Implicit lock: Each node has a unique parent
Each node in the hierarchy can be locked A requested node is locked in an explicit mode, e.g., exclusive lock Implicit lock: When a parent node is locked in an exclusive lock, each descendent is locked in an implicit lock
79
Intentional Lock Must be assigned top-down
Indicate intention (e.g., IS, IX) Must tag both the ancestors and the descendents of a node
80
Locks IS: intentional share the requested node
Descendents: can be explicitly locked in S or IS mode IX: intentional exclusive access to the requested node Descendents: can be locked explicitly in X, S, SIX, IS, and IX
81
Locks S: shared access to the requested node
Descendents: implicit shared locks on all X: exclusive access to the requested node Descendents: implicit x-locks on all SIX: shared and intentional exclusive access on the requested node Descendents: implicit shared lock on all, and explicit lock in X, SIX, or IX mode
82
Compatibility IS IX S SIX X
83
How does locking work in practice?
Every system is different (E.g., may not even provide CONFLICT-SERIALIZABLE schedules) But here is one (simplified) way ...
84
Sample Locking System:
(1) Don’t trust transactions to request/release locks (2) Hold all locks until transaction commits # locks time
85
l(A),Read(A),l(B),Write(B)… Scheduler, part I lock table
Read(A),Write(B) l(A),Read(A),l(B),Write(B)… Scheduler, part I lock table Scheduler, part II DB
86
Next class Distributed Database Systems
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.