cs4432transaction management1 cs4432: database systems ii lecture #23 transaction management...
Post on 22-Dec-2015
215 views
TRANSCRIPT
CS4432 transaction management 1
CS4432: Database Systems IILecture #23
Transaction Management
Professor Elke A. Rundensteiner
CS4432 transaction management 2
# locksheld byTi
Time Growing Shrinking Phase Phase
2 Phase Locking Protocol
CS4432 transaction management 3
Now :
Show that rules #1,2,3 conflict- serializable schedules
CS4432 transaction management 4
Conflict rules for li(A), ui(A):
• li(A), lj(A) conflict • li(A), uj(A) conflict
CS4432 transaction management 5
Theorem Rules #1,2,3 conflict (2PL) serializable
schedule
To help in proof:Definition Shrink(Ti) = SH(Ti) =
first unlock action of Ti
CS4432 transaction management 6
LemmaTi Tj in S SH(Ti) <S SH(Tj)
Proof of lemma:Ti Tj means that
S = … pi(A) … qj(A) …; p,q conflictBy rules 1,2:
S = … pi(A) … ui(A) … lj(A) ... qj(A) … SH(Ti) SH(Tj)
By rule 3:
So, SH(Ti) <S SH(Tj)
CS4432 transaction management 7
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
Theorem Rules #1,2,3 conflict (2PL) serializable
schedule
CS4432 transaction management 8
• Beyond this simple 2PL protocol, it is all a matter of improving
performance and allowing more concurrency….– Shared locks– Multiple granularity– Other types of C.C. mechanisms
CS4432 transaction management 9
Chapter 10 More on transaction processing
Topics:• Cascading rollback • Recoverable schedule• Deadlocks
– Prevention– Detection
CS4432 transaction management 10
Schedule H (T2 reversed)
T1 T2l1(A); Read(A) l2(B);Read(B)A A+100;Write(A) B Bx2;Write(B)
ll11(B)(B) l l22(A)(A)delayeddelayed
CS4432 transaction management 11
• 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!
CS4432 transaction management 12
Example: Ti To
Wi(A) rj(A) Commit To
Abort Ti
Concurrency control & recovery
……
… ……
… Cascading rollback (Bad!)
CS4432 transaction management 13
• Schedule is conflict serializable• Ti To
• But not recoverable
CS4432 transaction management 14
• Need to make “final’ decision for each transaction:– commit decision - system guarantees
transaction will or has completed, no matter what
– abort decision - system guarantees transaction will or has been rolled back
(has no effect)
CS4432 transaction management 15
To model this, two new actions:
• Ci - transaction Ti commits• Ai - transaction Ti aborts
CS4432 transaction management 16
...
...
...
...
Back to example:
Ti Tj
Wi(A)rj(A)
Cj can we commit here?
CS4432 transaction management 17
Definition
Ti read from Tj in S (Tj S Tj) if
(1) wj(A) <S ri(A)
(2) aj <S ri(A) (< : does not precede)
(3) If wj(A) <S wk(A) <S ri(A) then
ak <S ri(A)
CS4432 transaction management 18
Definition
Schedule S is recoverable if whenever Tj S Ti and j i and Ci
Sthen Cj <S Ci
CS4432 transaction management 19
Note: in transactions, reads and writes precede commit or abort
If Ci Ti, then ri(A) < Ci
wi(A) < Ci
If Ai Ti, then ri(A) < Ai
wi(A) < Ai
CS4432 transaction management 20
How to achieve recoverable schedules?
CS4432 transaction management 21
With 2PL, hold write locks to commit (strict 2PL)
Ti Tj
Wi(A)
Ciui(A)
rj(A)
...
...
...
...
...
...
...
CS4432 transaction management 22
• 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.
CS4432 transaction management 23
• S is strict if each transaction may read and write only items previously written by committed transactions.
Avoids cascading rollback
RC
ACA
ST SERIAL
CS4432 transaction management 24
CS4432 transaction management 25
Schedule with Deadlock
T1 T2l1(A); Read(A) l2(B);Read(B)A A+100;Write(A) B Bx2;Write(B)
ll11(B)(B) l l22(A)(A)delayeddelayed
CS4432 transaction management 26
Deadlocks
• Detection– Wait-for graph
• Prevention– Resource ordering– Timeout– Wait-die– Wound-wait
CS4432 transaction management 27
Deadlock Detection
• Build Wait-For graph• Use lock table structures• Build incrementally or periodically• When cycle found, rollback victim
T1
T3
T2
T6
T5
T4T7
CS4432 transaction management 28
Resource Ordering
• Order all elements A1, A2, …, An
• A transaction T can lock Ai after Aj only if i > j
Problem : Ordered lock requests not realistic in most cases
CS4432 transaction management 29
Timeout
• If transaction waits more than L sec., roll it back!
• Simple scheme• Hard to select L
CS4432 transaction management 30
Wait-die
• Transactions given a timestamp when they arrive …. ts(Ti)
• Ti can only wait for Tj if ts(Ti)< ts(Tj) ...else die
CS4432 transaction management 31
T1
(ts =10)
T2
(ts =20)
T3
(ts =25)
wait
wait
Example:
wait?
CS4432 transaction management 32
Wound-wait
• Transactions given a timestamp when they arrive … ts(Ti)
• Ti wounds Tj if ts(Ti)< ts(Tj) else Ti waits
“Wound”: Tj rolls back and gives lock to Ti
CS4432 transaction management 33
Summary
• Cascading rollbackRecoverable schedule
• Deadlock– Prevention– Detection