Download presentation
Presentation is loading. Please wait.
1
cs4432concurrency control1 CS4432: Database Systems II Concurrency Control with Recovery
2
cs4432concurrency control2 Example:T i T o W i (A) r j (A) Commit T o Abort T i Concurrency control & recovery … … … … … … Cascading rollback ( But already committed!)
3
cs4432concurrency control3 Note : Schedule is conflict serializable, But schedule is not recoverable.
4
cs4432concurrency control4 Need to make “final’ decision for each transaction: –commit decision - system guarantees transaction will or has completed, no matter what (Ci) –abort decision - system guarantees transaction will or has been rolled back (has no effect) (Ai)
5
cs4432concurrency control5 To model this, two new actions: C i - transaction T i commits A i - transaction T i aborts
6
cs4432concurrency control6... Back to example: T i T j W i (A) r j (A) C j can we commit here?
7
cs4432concurrency control7 Definition T i read from T j in S (T j S T j ) if (1)w j (A) < S r i (A) (2)a j < S r i (A) (< : does not precede ) (3) If w j (A) < S w k (A) < S r i (A) then a k < S r i (A)
8
cs4432concurrency control8 Definition Intuition : A schedule S is recoverable, if transactions only commit after all transactions they read from have already been committed first. Formal : Schedule S is recoverable if whenever Tj S Ti and j i and Ci S then Cj < S Ci
9
cs4432concurrency control9 Note: in transactions, reads and writes precede commit or abort If C i T i, then r i (A) < C i w i (A) < C i If A i T i, then r i (A) < A i w i (A) < A i
10
cs4432concurrency control10 How to achieve recoverable schedules?
11
cs4432concurrency control11 Strict 2PL : With 2PL, hold write locks until commit T i T j W i (A) Ci u i (A) r j (A)...
12
cs4432concurrency control12 S is recoverable if each transaction commits only after all transactions from which it read have committed. S avoids cascading rollback if each transaction may read only those values written by committed transactions.
13
cs4432concurrency control13 S is strict if each transaction may read and write only items previously written by committed transactions. Avoids cascading rollback RC ACR ST SERIAL
14
14 Examples Recoverable: –w 1 (A) w 1 (B) w 2 (A) r 2 (B) c 1 c 2 Avoids Cascading Rollback: –w 1 (A) w 1 (B) w 2 (A) c 1 r 2 (B) c 2 Strict: –w 1 (A) w 1 (B) c 1 w 2 (A) r 2 (B) c 2 Assumes w 2 (A) is done without reading
15
CS4432transaction management15 Schedule with Deadlock delayed
16
CS4432transaction management16 Deadlocks Detection –Wait-for graph Prevention –Resource ordering –Timeout –Wait-die –Wound-wait
17
CS4432transaction management17 Deadlock Detection Build Wait-For graph Use lock table structures Build incrementally or periodically When cycle found, rollback victim T1T1 T3T3 T2T2 T6T6 T5T5 T4T4 T7T7
18
CS4432transaction management18 Resource Ordering Order all elements A 1, A 2, …, A n A transaction T can lock A i after A j only if i > j Problem : Ordered lock requests not realistic in most cases
19
CS4432transaction management19 Timeout If transaction waits more than L sec., roll it back! Simple scheme Hard to select L
20
CS4432transaction management20 Wait-die Transactions given a timestamp when they arrive …. ts(T i ) T i can only wait for T j if ts(T i )< ts(T j )...else die
21
CS4432transaction management21 T 1 (ts =10) T 2 (ts =20) T 3 (ts =25) wait Example: wait?
22
CS4432transaction management22 Wound-wait Transactions given a timestamp when they arrive … ts(T i ) T i wounds T j if ts(T i )< ts(T j ) else T i waits “Wound”: T j rolls back and gives lock to T i
23
CS4432transaction management23 Summary Cascading rollback Recoverable schedule Deadlock –Prevention –Detection
24
Overall Summary cs4432concurrency control24
25
25 Recovery: DB Dump + Log backup database active database log If active database is lost, – restore active database from backup – bring up-to-date using redo entries in log
26
26 When can log be discarded? check- point db dump last needed undo not needed for media recovery not needed for undo after system failure not needed for redo after system failure log time
27
27 Multiple Mode Locks: Shared locks So far: S =...l 1 (A) r 1 (A) u 1 (A) … l 2 (A) r 2 (A) u 2 (A) … Do not conflict Instead: S=... ls 1 (A) r 1 (A) ls 2 (A) r 2 (A) …. us 1 (A) us 2 (A) Also, we have exclusive (X)/write locks
28
28 Ti Read(A),Write(B) l(A),Read(A),l(B),Write(B)… Read(A),Write(B) Scheduler, part I Scheduler, part II DB lock table Locks in Practice
29
29 Lock table Conceptually A B C ... Lock info for B Lock info for C If null, object is unlocked Every possible object
30
30 But use hash table: A If object not found in hash table, it is unlocked Lock info for A A... H
31
31 Lock info for A - example tran mode wait? Nxt T_link Object:A Group mode:U Waiting:yes List: T1 S no T2 U no T3X yes To other T3 records
32
32 What are the objects we lock? ? Relation A Relation B... Tuple A Tuple B Tuple C... Disk block A Disk block B... DB
33
33 Locking works in any case, but should we choose small or large objects? If we lock large objects (e.g., Relations) –Need few locks –Low concurrency If we lock small objects (e.g., tuples,fields) –Need more locks –More concurrency
34
34 We can have it both ways!! Ask any janitor to give you the solution... hall Stall 1Stall 2Stall 3Stall 4 restroom
35
35 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (S)
36
36 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (IX) T 2 (IX)
37
37 Multiple granularity CompRequestor IS IX S SIX X IS Holder IX S SIX X
38
38 Multiple granularity CompRequestor IS IX S SIX X IS Holder IX S SIX X TTTTF F F F FFFFF FFFT FTFT FFTT
39
39 Phantoms Inserting Records: Phantoms Example: relation R (E#,name,…) constraint: E# is key use tuple locking RE#Name…. o155Smith o275Jones
40
40 T 1 : Insert into R T 2 : Insert into R T 1 T 2 S 1 (o 1 ) S 2 (o 1 ) S 1 (o 2 ) S 2 (o 2 ) Check Constraint Insert o 3 [04,Kerry,..] Insert o 4 [04,Bush,..]...
41
41 Solution Use multiple granularity tree Before insert of node Q, lock parent(Q) in X mode R1 t1t1 t2t2 t3t3
42
42 Back to example T 1 : Insert T 2 : Insert T 1 T 2 X 1 (R) Check constraint Insert U(R) X 2 (R) Check constraint Oops! e# = 04 already in R! delayed
43
43 Instead of using R, can use index on R: Example: R Index 0<E#<100 Index 100<E#<200 E#=2E#=5 E#=107 E#=109...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.