Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner.

Similar presentations


Presentation on theme: "Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner."— Presentation transcript:

1 cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner

2 cs4432concurrency control2 Concepts Serial schedule: no interleaving of trans/actions S 1, S 2 are conflict equivalent schedules if S 1 transforms into S 2 by swaps on non- conflicting actions. A schedule is conflict serializable if it is conflict equivalent to some serial schedule.

3 cs4432concurrency control3 S 1 =w 1 (A) r 2 (A) w 2 (B) r 1 (B) S 2 =r 2 (A) w 1 (A) r 1 (B) w 2 (B)

4 cs4432concurrency control4 For a given schedule: Sd=r 1 (A)w 1 (A)r 2 (A)w 2 (A) r 2 (B)w 2 (B)r 1 (B)w 1 (B) T 1  T 2 T 2  T 1 T 1 T 2 If graph has cycles, Then schedule is not conflict serializable. Record dependencies in Precedence Graph:

5 cs4432concurrency control5 Theorem P(S 1 ) acyclic  S 1 conflict serializable

6 cs4432concurrency control6 How to enforce serializable schedules?

7 cs4432concurrency control7 How to enforce serializable schedules? Option 1: try all possible swaps of non- conflicting operation pairs to determine if the schedule can be turned into a serial (good) one.

8 cs4432concurrency control8 How to enforce serializable schedules? Option 2: a. run system, recording P(S). b. “at end of day”, check P(S) for cycles. c. declare if execution was good or not.

9 cs4432concurrency control9 Returning to Sc Sc=r 1 (A)w 1 (A)r 2 (A)w 2 (A)r 1 (B)w 1 (B)r 2 (B)w 2 (B) T 1  T 2 T 1  T 2  no cycles  Sc is “equivalent” to a serial schedule, I.e., in this case (T 1,T 2 ).

10 cs4432concurrency control10 For Schedule D: Sd=r 1 (A)w 1 (A)r 2 (A)w 2 (A) r 2 (B)w 2 (B)r 1 (B)w 1 (B) T 1  T 2 T 2  T 1 T 1 T 2 Sd cannot be rearranged into serial schedule

11 cs4432concurrency control11 Option 3: prevent P(S) cycles from ever occurring T 1 T 2 …..T n Scheduler DB How to enforce serializable schedules?

12 cs4432concurrency control12 A locking protocol Two new actions: lock (exclusive):l i (A) unlock:u i (A) scheduler T 1 T 2 lock table

13 cs4432concurrency control13 Rule #1: Well-formed transactions T i : … l i (A) … p i (A) … u i (A)...

14 cs4432concurrency control14 Rule #2 Legal scheduler S = …….. l i (A) ………... u i (A) ……... no l j (A)

15 cs4432concurrency control15 What schedules are legal? What transactions are well-formed? S1 = l 1 (A)l 1 (B)r 1 (A)w 1 (B)l 2 (B)u 1 (A)u 1 (B) r 2 (B)w 2 (B)u 2 (B)l 3 (B)r 3 (B)u 3 (B) S2 = l 1 (A)r 1 (A)w 1 (B)u 1 (A)u 1 (B) l 2 (B)r 2 (B)w 2 (B)l 3 (B)r 3 (B)u 3 (B) S3 = l 1 (A)r 1 (A)u 1 (A)l 1 (B)w 1 (B)u 1 (B) l 2 (B)r 2 (B)w 2 (B)u 2 (B)l 3 (B)r 3 (B)u 3 (B) Exercise:

16 cs4432concurrency control16 What schedules are legal? What transactions are well-formed? S1 = l 1 (A)l 1 (B)r 1 (A)w 1 (B)l 2 (B)u 1 (A)u 1 (B) r 2 (B)w 2 (B)u 2 (B)l 3 (B)r 3 (B)u 3 (B) S2 = l 1 (A)r 1 (A)w 1 (B)u 1 (A)u 1 (B) l 2 (B)r 2 (B)w 2 (B)l 3 (B)r 3 (B)u 3 (B) S3 = l 1 (A)r 1 (A)u 1 (A)l 1 (B)w 1 (B)u 1 (B) l 2 (B)r 2 (B)w 2 (B)u 2 (B)l 3 (B)r 3 (B)u 3 (B) Exercise:

17 cs4432concurrency control17 Schedule F : Let’s Add Some Locking! T1 T2 l 1 (A);Read(A) A A+100;Write(A);u 1 (A) l 2 (A);Read(A) A Ax2;Write(A);u 2 (A) l 2 (B);Read(B) B Bx2;Write(B);u 2 (B) l 1 (B);Read(B) B B+100;Write(B);u 1 (B)

18 cs4432concurrency control18 Schedule F A B T1 T2 25 25 l 1 (A);Read(A) A A+100;Write(A);u 1 (A) 125 l 2 (A);Read(A) A Ax2;Write(A);u 2 (A) 250 l 2 (B);Read(B) B Bx2;Write(B);u 2 (B) 50 l 1 (B);Read(B) B B+100;Write(B);u 1 (B) 150 250 150

19 cs4432concurrency control19 Rule #3 Two phase locking (2PL) for transactions T i = ……. l i (A) ………... u i (A) ……... no unlocks no locks

20 cs4432concurrency control20 # locks held by Ti Time Growing Shrinking Phase Phase 2 Phase Locking Protocol

21 cs4432concurrency control21 Schedule F : Does it follow 2PL ? T1 T2 l 1 (A);Read(A) A A+100;Write(A);u 1 (A) l 2 (A);Read(A) A Ax2;Write(A);u 2 (A) l 2 (B);Read(B) B Bx2;Write(B);u 2 (B) l 1 (B);Read(B) B B+100;Write(B);u 1 (B)

22 cs4432concurrency control22 Schedule G T1 T2 l 1 (A);Read(A) A A+100;Write(A) l 1 (B); u 1 (A) l 2 (A);Read(A) A Ax2;Write(A);

23 cs4432concurrency control23 Schedule G delayed

24 cs4432concurrency control24 Schedule G delayed

25 cs4432concurrency control25 Schedule G delayed

26 cs4432concurrency control26 We Got the GOOD Schedule !!!! T1T2 Read(A); A  A+100 Write(A); Read(A);A  A  2; Write(A); Read(B); B  B+100; Write(B); Read(B);B  B  2; Write(B); AB25 125 250 125 250250

27 cs4432concurrency control27 Now : Show that protocol with rules #1,2,3  conflict serializable schedules

28 cs4432concurrency control28 Conflict rules for l i (A), u i (A): l i (A), l j (A) conflict l i (A), u j (A) conflict

29 cs4432concurrency control29 Theorem Rules #1,2,3  conflict (2PL) serializable schedule To help in proof: Definition Shrink(Ti) = SH(Ti) = first unlock action of Ti

30 cs4432concurrency control30 Lemma Ti  Tj in S  SH(Ti) < S SH(Tj) Proof of Lemma: Ti  Tj means that S = … p i (A) … q j (A) …; p,q conflict By rules 1,2: S = … p i (A) … u i (A) … l j (A)... q j (A) … SH(Ti) SH(Tj) By rule 3: So, SH(Ti) < S SH(Tj)

31 cs4432concurrency control31 Proof: (1) Assume P(S) has cycle T 1  T 2  …. T n  T 1 (2) By lemma: SH(T 1 ) < SH(T 2 ) <... < SH(T 1 ) (3) Impossible, so P(S) acyclic (4)  S is conflict serializable Theorem Rules #1,2,3  conflict (2PL) serializable schedule

32 cs4432concurrency control32 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


Download ppt "Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner."

Similar presentations


Ads by Google