Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 What we have covered? uIndexing and Hashing uData warehouse and OLAP uData Mining uInformation Retrieval and Web Mining uXML and XQuery uSpatial Databases.

Similar presentations


Presentation on theme: "1 What we have covered? uIndexing and Hashing uData warehouse and OLAP uData Mining uInformation Retrieval and Web Mining uXML and XQuery uSpatial Databases."— Presentation transcript:

1 1 What we have covered? uIndexing and Hashing uData warehouse and OLAP uData Mining uInformation Retrieval and Web Mining uXML and XQuery uSpatial Databases uTransaction Management

2 2 Lecture 7: Transactions

3 3 The Setting uDatabase systems are normally being accessed by many users or processes at the same time. wBoth queries and modifications. uUnlike operating systems, which support interaction of processes, a DMBS needs to keep processes from troublesome interactions.

4 4 Example: Bad Interaction uYou and your domestic partner each take $100 from different ATM’s at about the same time. wThe DBMS better make sure one account deduction doesn’t get lost. uCompare: An OS allows two people to edit a document at the same time. If both write, one’s changes get lost.

5 5 ACID Transactions uA DBMS is expected to support “ACID transactions,” processes that are: wAtomic : Either the whole process is done or none is. wConsistent : Database constraints are preserved. wIsolated : It appears to the user as if only one process executes at a time. wDurable : Effects of a process do not get lost if the system crashes.

6 6 Transactions in SQL uSQL supports transactions, often behind the scenes. wEach statement issued at the generic query interface is a transaction by itself. wIn programming interfaces like Embedded SQL or PSM, a transaction begins the first time a SQL statement is executed and ends with the program or an explicit transaction- end.

7 7 COMMIT uThe SQL statement COMMIT causes a transaction to complete. wIt’s database modifications are now permanent in the database.

8 8 ROLLBACK uThe SQL statement ROLLBACK also causes the transaction to end, but by aborting. wNo effects on the database. uFailures like division by 0 or a constraint violation can also cause rollback, even if the programmer does not request it.

9 9 An Example: Interacting Processes uAssume the usual Sells(bar,beer,price) relation, and suppose that Joe’s Bar sells only Bud for $2.50 and Miller for $3.00. uSally is querying Sells for the highest and lowest price Joe charges. uJoe decides to stop selling Bud and Miller, but to sell only Heineken at $3.50.

10 10 Sally’s Program uSally executes the following two SQL statements, which we call (min) and (max), to help remember what they do. (max)SELECT MAX(price) FROM Sells WHERE bar = ’Joe’’s Bar’; (min)SELECT MIN(price) FROM Sells WHERE bar = ’Joe’’s Bar’;

11 11 Joe’s Program uAt about the same time, Joe executes the following steps, which have the mnemonic names (del) and (ins). (del) DELETE FROM Sells WHERE bar = ’Joe’’s Bar’; (ins) INSERT INTO Sells VALUES(’Joe’’s Bar’, ’Heineken’, 3.50);

12 12 Interleaving of Statements uAlthough (max) must come before (min), and (del) must come before (ins), there are no other constraints on the order of these statements, unless we group Sally’s and/or Joe’s statements into transactions.

13 13 Example: Strange Interleaving uSuppose the steps execute in the order (max)(del)(ins)(min). Joe’s Prices: Statement: Result: uSally sees MAX < MIN! 2.50, 3.00 (del) (ins) 3.50 (min) 3.50 2.50, 3.00 (max) 3.00

14 14 Fixing the Problem by Using Transactions uIf we group Sally’s statements (max)(min) into one transaction, then she cannot see this inconsistency. uShe sees Joe’s prices at some fixed time. wEither before or after he changes prices, or in the middle, but the MAX and MIN are computed from the same prices.

15 15 Another Problem: Rollback uSuppose Joe executes (del)(ins), not as a transaction, but after executing these statements, thinks better of it and issues a ROLLBACK statement. uIf Sally executes her statements after (ins) but before the rollback, she sees a value, 3.50, that never existed in the database.

16 16 Solution uIf Joe executes (del)(ins) as a transaction, its effect cannot be seen by others until the transaction executes COMMIT. wIf the transaction executes ROLLBACK instead, then its effects can never be seen.

17 17 Isolation Levels uSQL defines four isolation levels = choices about what interactions are allowed by transactions that execute at about the same time. uHow a DBMS implements these isolation levels is highly complex, and a typical DBMS provides its own options.

18 18 Choosing the Isolation Level uWithin a transaction, we can say: SET TRANSACTION ISOLATION LEVEL X where X = 1.SERIALIZABLE 2.REPEATABLE READ 3.READ COMMITTED 4.READ UNCOMMITTED

19 19 Serializable Transactions uIf Sally = (max)(min) and Joe = (del)(ins) are each transactions, and Sally runs with isolation level SERIALIZABLE, then she will see the database either before or after Joe runs, but not in the middle. uIt’s up to the DBMS vendor to figure out how to do that, e.g.: wTrue isolation in time. wKeep Joe’s old prices around to answer Sally’s queries.

20 20 Isolation Level Is Personal Choice uYour choice, e.g., run serializable, affects only how you see the database, not how others see it. uExample: If Joe Runs serializable, but Sally doesn’t, then Sally might see no prices for Joe’s Bar. wi.e., it looks to Sally as if she ran in the middle of Joe’s transaction.

21 21 Read-Commited Transactions uIf Sally runs with isolation level READ COMMITTED, then she can see only committed data, but not necessarily the same data each time. uExample: Under READ COMMITTED, the interleaving (max)(del)(ins)(min) is allowed, as long as Joe commits. wSally sees MAX < MIN.

22 22 Repeatable-Read Transactions uRequirement is like read-committed, plus: if data is read again, then everything seen the first time will be seen the second time. wBut the second and subsequent reads may see more tuples as well.

23 23 Example: Repeatable Read uSuppose Sally runs under REPEATABLE READ, and the order of execution is (max)(del)(ins)(min). w(max) sees prices 2.50 and 3.00. w(min) can see 3.50, but must also see 2.50 and 3.00, because they were seen on the earlier read by (max).

24 24 Read Uncommitted uA transaction running under READ UNCOMMITTED can see data in the database, even if it was written by a transaction that has not committed (and may never). uExample: If Sally runs under READ UNCOMMITTED, she could see a price 3.50 even if Joe later aborts.

25 25 Concurrency Control T1T2…Tn DB (consistency constraints)

26 26 Review uWhy do we need transaction? uWhat’s ACID? uWhat’s SQL support for transaction? uWhat’s the four isolation level wSERIALIZABLE wREPEATABLE READ wREAD COMMITTED wREAD UNCOMMITTED

27 27 Example: T1:Read(A)T2:Read(A) A  A+100A  A  2Write(A)Read(B) B  B+100B  B  2Write(B) Constraint: A=B

28 28 Schedule A T1T2 Read(A); A  A+100 Write(A); Read(B); B  B+100; Write(B); Read(A);A  A  2; Write(A); Read(B);B  B  2; Write(B); AB25 125 250 250

29 29 Schedule B T1T2 Read(A);A  A  2; Write(A); Read(B);B  B  2; Write(B); Read(A); A  A+100 Write(A); Read(B); B  B+100; Write(B); AB25 50 150 150

30 30 Schedule C 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

31 31 Schedule D T1T2 Read(A); A  A+100 Write(A); Read(A);A  A  2; Write(A); Read(B);B  B  2; Write(B); Read(B); B  B+100; Write(B); AB25 125 250 50 150 250150

32 32 Schedule E T1T2’ Read(A); A  A+100 Write(A); Read(A);A  A  1; Write(A); Read(B);B  B  1; Write(B); Read(B); B  B+100; Write(B); AB25 125 25 125125 Same as Schedule D but with new T2’

33 33 uWant schedules that are “good”, regardless of winitial state and wtransaction semantics uOnly look at order of read and writes Example: 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)

34 34 Sc’=r 1 (A)w 1 (A) r 1 (B)w 1 (B)r 2 (A)w 2 (A)r 2 (B)w 2 (B) T 1 T 2 Example: 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)

35 35 Review uWhy do we need transaction? uWhat’s ACID? uWhat’s SQL support for transaction? uWhat’s the four isolation level wSERIALIZABLE wREPEATABLE READ wREAD COMMITTED wREAD UNCOMMITTED

36 36 Example: T1:Read(A)T2:Read(A) A  A+100A  A  2Write(A)Read(B) B  B+100B  B  2Write(B) Constraint: A=B

37 37 Schedule D T1T2 Read(A); A  A+100 Write(A); Read(A);A  A  2; Write(A); Read(B);B  B  2; Write(B); Read(B); B  B+100; Write(B); AB25 125 250 50 150 250150

38 38 However, for Sd: 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) uas a matter of fact, T 2 must precede T 1 in any equivalent schedule, i.e., T 2  T 1

39 39 T 1 T 2 Sd cannot be rearranged into a serial schedule Sd is not “equivalent” to any serial schedule Sd is “bad” u T 2  T 1 u Also, T 1  T 2

40 40 Schedule C 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

41 41 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 (in this case T 1,T 2 )

42 42 Concepts Transaction: sequence of r i (x), w i (x) actions Conflicting actions: r 1(A) w 2(A) w 1(A) w 2(A) r 1(A) w 2(A) Schedule: represents chronological order in which actions are executed Serial schedule: no interleaving of actions or transactions

43 43 Definition S 1, S 2 are conflict equivalent schedules if S 1 can be transformed into S 2 by a series of swaps on non-conflicting actions.

44 44 Definition A schedule is conflict serializable if it is conflict equivalent to some serial schedule.

45 45 Nodes: transactions in S Arcs: Ti  Tj whenever - p i (A), q j (A) are actions in S - p i (A) < S q j (A) - at least one of p i, q j is a write Precedence graph P(S) (S is schedule )

46 46 Exercise: uWhat is P(S) for S = w 3 (A) w 2 (C) r 1 (A) w 1 (B) r 1 (C) w 2 (A) r 4 (A) w 4 (D) uIs S serializable?

47 47 Another Exercise: uWhat is P(S) for S = w 1 (A) r 2 (A) r 3 (A) w 4 (A) ?

48 48 Lemma S 1, S 2 conflict equivalent  P(S 1 )=P(S 2 ) Proof: Assume P(S 1 )  P(S 2 )   T i : T i  T j in S 1 and not in S 2  S 1 = …p i (A)... q j (A)… p i, q j S 2 = …q j (A)…p i (A)... conflict  S 1, S 2 not conflict equivalent

49 49 Note: P(S 1 )=P(S 2 )  S 1, S 2 conflict equivalent Counter example: 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)

50 50 Theorem P(S 1 ) acyclic  S 1 conflict serializable (  ) Assume S 1 is conflict serializable   S s : S s, S 1 conflict equivalent  P(S s ) = P(S 1 )  P(S 1 ) acyclic since P(S s ) is acyclic

51 51 (  ) Assume P(S 1 ) is acyclic Transform S 1 as follows: (1) Take T 1 to be transaction with no incident arcs (2) Move all T 1 actions to the front S 1 = ……. q j (A)……. p 1 (A)….. (3) we now have S 1 = (4) repeat above steps to serialize rest! T 1 T 2 T 3 T 4 Theorem P(S 1 ) acyclic  S 1 conflict serializable

52 52 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

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

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

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

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

57 57 uWhat 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:

58 58 uWhat 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:

59 59 Schedule F 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)

60 60 Schedule F 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 A B

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

62 62 # locks held by Ti Time Growing Shrinking Phase Phase

63 63 Schedule G delayed

64 64 Schedule G delayed

65 65 Schedule G delayed

66 66 Schedule H (T 2 reversed) delayed

67 67 uAssume deadlocked transactions are rolled back wThey have no effect wThey do not appear in schedule E.g., Schedule H = This space intentionally left blank!

68 68 Next step: Show that rules #1,2,3  conflict- serializable schedules

69 69 Conflict rules for l i (A), u i (A): ul i (A), l j (A) conflict ul i (A), u j (A) conflict Note: no conflict,,...

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

71 71 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) … By rule 3: SH(Ti) SH(Tj) So, SH(Ti) < S SH(Tj)

72 72 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

73 73 uBeyond this simple 2PL protocol, it is all a matter of improving performance and allowing more concurrency…. wShared locks wMultiple granularity wInserts, deletes and phantoms wOther types of C.C. mechanisms

74 74 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)

75 75 Lock actions l-t i (A): lock A in t mode (t is S or X) u-t i (A): unlock t mode (t is S or X) Shorthand: u i (A): unlock whatever modes T i has locked A

76 76 Rule #1 Well formed transactions T i =... l-S 1 (A) … r 1 (A) … u 1 (A) … T i =... l-X 1 (A) … w 1 (A) … u 1 (A) …

77 77 uWhat about transactions that read and write same object? Option 1: Request exclusive lock T i =...l-X 1 (A) … r 1 (A)... w 1 (A)... u(A) …

78 78 Option 2: Upgrade (E.g., need to read, but don’t know if will write…) T i =... l-S 1 (A) … r 1 (A)... l-X 1 (A) …w 1 (A)...u(A)… Think of - Get 2nd lock on A, or - Drop S, get X lock What about transactions that read and write same object?

79 79 Rule #2 Legal scheduler S =....l-S i (A) … … u i (A) … no l-X j (A) S =... l-X i (A) … … u i (A) … no l-X j (A) no l-S j (A)

80 80 A way to summarize Rule #2 Compatibility matrix Comp S X S true false Xfalse false

81 81 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

82 82 Proof: similar to X locks case Detail: l-t i (A), l-r j (A) do not conflict if comp(t,r) l-t i (A), u-r j (A) do not conflict if comp(t,r) Theorem Rules 1,2,3  Conf.serializable for S/X locks schedules

83 83 Lock types beyond S/X Examples: (1) increment lock (2) update lock

84 84 Example (1): increment lock uAtomic increment action: IN i (A) {Read(A); A  A+k; Write(A)} uIN i (A), IN j (A) do not conflict! A=7 A=5A=17 A=15 IN i (A) +2 IN j (A) +10 IN j (A) +2 IN i (A)

85 85 CompSXI S X I

86 86 CompSXI STFF XFFF IFFT

87 87 Update locks

88 88 Solution If T i wants to read A and knows it may later want to write A, it requests update lock (not shared)

89 89 CompSXU S X U New request Lock already held in

90 90 CompSXU STFT XFFF U TorF FF -> symmetric table? New request Lock already held in

91 91 Note: object A may be locked in different modes at the same time... S 1 =...l-S 1 (A)…l-S 2 (A)…l-U 3 (A)… l-S 4 (A)…? l-U 4 (A)…? uTo grant a lock in mode t, mode t must be compatible with all currently held locks on object

92 92 How does locking work in practice? uEvery system is different (E.g., may not even provide CONFLICT-SERIALIZABLE schedules) uBut here is one (simplified) way...

93 93 (1) Don’t trust transactions to request/release locks (2) Hold all locks until transaction commits # locks time Sample Locking System:

94 94 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

95 95 Lock table Conceptually A  B C ... Lock info for B Lock info for C If null, object is unlocked Every possible object

96 96 But use hash table: A If object not found in hash table, it is unlocked Lock info for A A... H

97 97 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

98 98 What are the objects we lock? ? Relation A Relation B... Tuple A Tuple B Tuple C... Disk block A Disk block B... DB

99 99 uLocking works in any case, but should we choose small or large objects? uIf we lock large objects (e.g., Relations) wNeed few locks wLow concurrency uIf we lock small objects (e.g., tuples,fields) wNeed more locks wMore concurrency

100 100 We can have it both ways!! Ask any janitor to give you the solution... hall Stall 1Stall 2Stall 3Stall 4 restroom

101 101 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (S)

102 102 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (IX) T 2 (IX)

103 103 Multiple granularity CompRequestor IS IX S SIX X IS Holder IX S SIX X

104 104 Multiple granularity CompRequestor IS IX S SIX X IS Holder IX S SIX X TTTTF F F F FFFFF FFFT FTFT FFTT

105 105 ParentChild can belocked in IS IX S SIX X P C

106 106 ParentChild can belocked in IS IX S SIX X P C IS, S IS, S, IX, X, SIX [S, IS] not necessary X, IX, [SIX] none

107 107 Rules (1) Follow multiple granularity comp function (2) Lock root of tree first, any mode (3) Node Q can be locked by Ti in S or IS only if parent(Q) locked by Ti in IX or IS (4) Node Q can be locked by Ti in X,SIX,IX only if parent(Q) locked by Ti in IX,SIX (5) Ti is two-phase (6) Ti can unlock node Q only if none of Q’s children are locked by Ti

108 108 Exercise: uCan T 2 access object f 2.2 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (IX) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (IX) T 1 (X)

109 109 Exercise: uCan T 2 access object f 2.2 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (X) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (IX)

110 110 Exercise: uCan T 2 access object f 3.1 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (S) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (IS)

111 111 Exercise: uCan T 2 access object f 2.2 in S mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (IX) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (SIX) T 1 (X)

112 112 Exercise: uCan T 2 access object f 2.2 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (IX) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (SIX) T 1 (X)

113 113 Insert + delete operations Insert A Z ...

114 114 Modifications to locking rules: (1) Get exclusive lock on A before deleting A (2) At insert A operation by Ti, Ti is given exclusive lock on A

115 115 Phantoms Still have a problem: Phantoms Example: relation R (E#,name,…) constraint: E# is key use tuple locking RE#Name…. o155Smith o275Jones

116 116 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,..]...

117 117 Solution uUse multiple granularity tree uBefore insert of node Q, lock parent(Q) in X mode R1 t1t1 t2t2 t3t3

118 118 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

119 119 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...

120 120 uThis approach can be generalized to multiple indexes...

121 121 Next: uTree-based concurrency control uValidation concurrency control

122 122 Example A B C D EF all objects accessed through root, following pointers T 1 lock  can we release A lock if we no longer need A??

123 123 Idea: traverse like “Monkey Bars” A B C D EF T 1 lock

124 124 Why does this work? uAssume all T i start at root; exclusive lock uT i  T j  T i locks root before T j uActually works if we don’t always start at root Root Q T i  T j

125 125 Rules: tree protocol (exclusive locks) (1) First lock by T i may be on any item (2) After that, item Q can be locked by T i only if parent(Q) locked by T i (3) Items may be unlocked at any time (4) After T i unlocks Q, it cannot relock Q

126 126 uTree-like protocols are used typically for B-tree concurrency control E.g., during insert, do not release parent lock, until you are certain child does not have to split Root

127 127 Validation Transactions have 3 phases: (1) Read wall DB values read wwrites to temporary storage wno locking (2) Validate wcheck if schedule so far is serializable (3) Write wif validate ok, write to DB

128 128 Key idea uMake validation atomic uIf T 1, T 2, T 3, … is validation order, then resulting schedule will be conflict equivalent to S s = T 1 T 2 T 3...

129 129 To implement validation, system keeps two sets: uFIN = transactions that have finished phase 3 (and are all done) uVAL = transactions that have successfully finished phase 2 (validation)

130 130 Example of what validation must prevent: RS(T 2 )={B} RS(T 3 )={A,B} WS(T 2 )={B,D} WS(T 3 )={C} time T 2 start T 2 validated T 3 validated T 3 start  = 

131 131 T 2 finish phase 3 Example of what validation must prevent: RS(T 2 )={B} RS(T 3 )={A,B} WS(T 2 )={B,D} WS(T 3 )={C} time T 2 start T 2 validated T 3 validated T 3 start  =  allow T 3 start

132 132 Another thing validation must prevent: RS(T 2 )={A} RS(T 3 )={A,B} WS(T 2 )={D,E} WS(T 3 )={C,D} time T 2 validated T 3 validated finish T 2 BAD: w 3 (D) w 2 (D)

133 133 finish T 2 Another thing validation must prevent: RS(T 2 )={A} RS(T 3 )={A,B} WS(T 2 )={D,E} WS(T 3 )={C,D} time T 2 validated T 3 validated allow finish T 2

134 134 Validation rules for T j : (1) When T j starts phase 1: ignore(T j )  FIN (2) at T j Validation: if check (T j ) then [ VAL  VAL U {T j }; do write phase; FIN  FIN U {T j } ]

135 135 Check (T j ): For T i  VAL - IGNORE (T j ) DO IF [ WS(T i )  RS(T j )   OR T i  FIN ] THEN RETURN false; RETURN true; Is this check too restrictive ?

136 136 Improving Check(T j ) For T i  VAL - IGNORE (T j ) DO IF [ WS(T i )  RS(T j )   OR ( T i  FIN AND WS(T i )  WS(T j )   )] THEN RETURN false; RETURN true;

137 137 Exercise: T: RS(T)={A,B} WS(T)={A,C} V: RS(V)={B} WS(V)={D,E} U: RS(U)={B} WS(U)={D} W: RS(W)={A,D} WS(W)={A,C} start validate finish

138 138 Validation (also called optimistic concurrency control) is useful in some cases: - Conflicts rare - System resources plentiful - Have real time constraints

139 139 Summary Have studied C.C. mechanisms used in practice - 2 PL - Multiple granularity - Tree (index) protocols - Validation

140 140 What about concurrent actions? Ti issuesSystemInput(X) t  x read(x,t)issuescompletes input(x) time T2 issues write(B,S) System issues input(B) completes B  S System issues output(B) completes

141 141 So net effect is either u S=…r 1 (x)…w 2 (b)… or u S=…w 2 (B)…r 1 (x)…

142 142 uAssume equivalent to either r 1 (A) w 2 (A) orw 2 (A) r 1 (A) u  low level synchronization mechanism uAssumption called “atomic actions” What about conflicting, concurrent actions on same object? start r 1 (A)end r 1 (A) start w 2 (A) end w 2 (A) time


Download ppt "1 What we have covered? uIndexing and Hashing uData warehouse and OLAP uData Mining uInformation Retrieval and Web Mining uXML and XQuery uSpatial Databases."

Similar presentations


Ads by Google