33
CS4432 transaction management 1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

  • View
    215

  • Download
    2

Embed Size (px)

Citation preview

Page 1: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 1

CS4432: Database Systems IILecture #23

Transaction Management

Professor Elke A. Rundensteiner

Page 2: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 2

# locksheld byTi

Time Growing Shrinking Phase Phase

2 Phase Locking Protocol

Page 3: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 3

Now :

Show that rules #1,2,3 conflict- serializable schedules

Page 4: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 4

Conflict rules for li(A), ui(A):

• li(A), lj(A) conflict • li(A), uj(A) conflict

Page 5: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 6: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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)

Page 7: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 8: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 9: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 9

Chapter 10 More on transaction processing

Topics:• Cascading rollback • Recoverable schedule• Deadlocks

– Prevention– Detection

Page 10: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 11: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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!

Page 12: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 12

Example: Ti To

Wi(A) rj(A) Commit To

Abort Ti

Concurrency control & recovery

……

… ……

… Cascading rollback (Bad!)

Page 13: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 13

• Schedule is conflict serializable• Ti To

• But not recoverable

Page 14: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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)

Page 15: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 15

To model this, two new actions:

• Ci - transaction Ti commits• Ai - transaction Ti aborts

Page 16: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 16

...

...

...

...

Back to example:

Ti Tj

Wi(A)rj(A)

Cj can we commit here?

Page 17: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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)

Page 18: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 18

Definition

Schedule S is recoverable if whenever Tj S Ti and j i and Ci

Sthen Cj <S Ci

Page 19: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 20: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 20

How to achieve recoverable schedules?

Page 21: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 21

With 2PL, hold write locks to commit (strict 2PL)

Ti Tj

Wi(A)

Ciui(A)

rj(A)

...

...

...

...

...

...

...

Page 22: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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.

Page 23: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 24: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 24

Page 25: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 26: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 26

Deadlocks

• Detection– Wait-for graph

• Prevention– Resource ordering– Timeout– Wait-die– Wound-wait

Page 27: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 28: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 29: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 29

Timeout

• If transaction waits more than L sec., roll it back!

• Simple scheme• Hard to select L

Page 30: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 31: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 31

T1

(ts =10)

T2

(ts =20)

T3

(ts =25)

wait

wait

Example:

wait?

Page 32: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

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

Page 33: CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner

CS4432 transaction management 33

Summary

• Cascading rollbackRecoverable schedule

• Deadlock– Prevention– Detection