CS 277 – Spring 2002Notes 091 CS 277: Database System Implementation Notes 09: Concurrency Control Arthur Keller.

Slides:



Advertisements
Similar presentations
1 Shivnath Babu Concurrency Control (II) CS216: Data-Intensive Computing Systems.
Advertisements

Cs4432concurrency control1 CS4432: Database Systems II Lecture #21 Concurrency Control : Theory Professor Elke A. Rundensteiner.
Database Systems (資料庫系統)
1 Concurrency Control Chapter Conflict Serializable Schedules  Two actions are in conflict if  they operate on the same DB item,  they belong.
1 Concurrency Control Conflict serializability Two phase locking Optimistic concurrency control Source: slides by Hector Garcia-Molina.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control Professor Elke A. Rundensteiner.
Concurrency Control II
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 2: Enforcing Serializable Schedules Professor Chen Li.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #23 Concurrency Control Professor Elke A. Rundensteiner.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner.
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 4: More on Locks Professor Chen Li.
1 CS216 Advanced Database Systems Shivnath Babu Notes 12: Concurrency Control (II)
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
Concurrency Control II. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Physical Design.
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 5: Tree-based Concurrency Control and Validation Currency Control Professor.
Concurrency Control Part 2 R&G - Chapter 17 The sequel was far better than the original! -- Nobody.
Quick Review of Apr 29 material
Concurrent Transactions Even when there is no “failure,” several transactions can interact to turn a consistent state into an inconsistent state.
Concurrency Control R&G - Chapter 17 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
Concurrency Control. Example Schedules Constraint: The sum of A+B must be the same Before: After: T1 read(A) A = A -50 write(A) read(B)
Concurrency. Busy, busy, busy... In production environments, it is unlikely that we can limit our system to just one user at a time. – Consequently, it.
Transactions or Concurrency Control. Introduction A program which operates on a DB performs 2 kinds of operations: –Access to the Database (Read/Write)
Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)
Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control with Recovery.
CS 245Notes 091 CS 245: Database System Principles Notes 09: Concurrency Control Hector Garcia-Molina.
Transactions Amol Deshpande CMSC424. Today Project stuff… Summer Internships 
Cs4432concurrency control1 CS4432: Database Systems II Lecture #21 Concurrency Control Professor Elke A. Rundensteiner.
CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner.
Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control.
Concurrency Control. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Transaction Processing.
CS4432: Database Systems II Transaction Management Motivation 1.
Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,
CS 245Notes 091 CS 245: Database System Principles Notes 09: Concurrency Control Hector Garcia-Molina.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner.
1 Notes 09: Transaction Processing Slides are modified from the CS 245 class slides of Hector Garcia- Molina.
CS 245Notes 091 CS 245: Database System Principles Notes 09: Concurrency Control Hector Garcia-Molina.
DBMS 2001Notes 8: Concurrency1 Principles of Database Management Systems 8: Concurrency Control Pekka Kilpeläinen (after Stanford CS245 slide originals.
TRANSACTIONS. Objectives Transaction Concept Transaction State Concurrent Executions Serializability Recoverability Implementation of Isolation Transaction.
1 CSE232A: Database System Principle Concurrency Control.
1 What we have covered? uIndexing and Hashing uData warehouse and OLAP uData Mining uInformation Retrieval and Web Mining uXML and XQuery uSpatial Databases.
Chapter 9 Concurrency Control. Contents Concurrency Control Concurrency Control by Locks Concurrency Control by Timestamps Concurrency Control by Validation.
V. Megalooikonomou Concurrency control (based on slides by C. Faloutsos at CMU and on notes by Silberchatz,Korth, and Sudarshan) Temple University – CIS.
Chapter 11 Concurrency Control. Lock-Based Protocols  A lock is a mechanism to control concurrent access to a data item  Data items can be locked in.
1 Concurrency Control II: Locking and Isolation Levels.
1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner.
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
CSE544: Transactions Concurrency Control Wednesday, 4/26/2006.
1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.
Jinze Liu. Option 1: run system, recording P(S); at end of day, check for P(S) cycles and declare if execution was good.
Jinze Liu. Tree-based concurrency control Validation concurrency control.
1 Ullman et al. : Database System Principles Notes 09: Concurrency Control.
Jinze Liu. ACID Atomicity: TX’s are either completely done or not done at all Consistency: TX’s should leave the database in a consistent state Isolation:
CS 440 Database Management Systems Concurrency Control 1.
Chapter 91 Chapter 9. Concurrency Control Fall 2001 Prof. Sang Ho Lee School of Computing, Soongsil Univ.
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
CS 540 Database Management Systems Concurrency Control 1.
CS 440 Database Management Systems
Concurrency Control.
Database Systems II Concurrency Control
CS 245: Database System Principles Notes 09: Concurrency Control
CS 245: Database System Principles Notes 09: Concurrency Control
Concurrency Control 11/22/2018.
Yan Huang - CSCI5330 Database Implementation – Concurrency Control
Notes 09: Transaction Processing
CPSC-608 Database Systems
Temple University – CIS Dept. CIS661 – Principles of Data Management
CPSC-608 Database Systems
CS 245: Database System Principles Notes 09: Concurrency Control
Data-Intensive Computing Systems
Lecture 18: Concurrency Control
Presentation transcript:

CS 277 – Spring 2002Notes 091 CS 277: Database System Implementation Notes 09: Concurrency Control Arthur Keller

CS 277 – Spring 2002Notes 092 Chapter 18Concurrency Control T1T2…Tn DB (consistency constraints)

CS 277 – Spring 2002Notes 093 Example: T1:Read(A)T2:Read(A) A  A+100A  A  2Write(A)Read(B) B  B+100B  B  2Write(B) Constraint: A=B

CS 277 – Spring 2002Notes 094 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); AB

CS 277 – Spring 2002Notes 095 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); AB

CS 277 – Spring 2002Notes 096 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); AB

CS 277 – Spring 2002Notes 097 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); AB

CS 277 – Spring 2002Notes 098 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); AB Same as Schedule D but with new T2’

CS 277 – Spring 2002Notes 099 Want schedules that are “good”, regardless of –initial state and –transaction semantics Only 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)

CS 277 – Spring 2002Notes 0910 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)

CS 277 – Spring 2002Notes 0911 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) as a matter of fact, T 2 must precede T 1 in any equivalent schedule, i.e., T 2  T 1

CS 277 – Spring 2002Notes 0912 T 1 T 2 Sd cannot be rearranged into a serial schedule Sd is not “equivalent” to any serial schedule Sd is “bad” T 2  T 1 Also, T 1  T 2

CS 277 – Spring 2002Notes 0913 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 )

CS 277 – Spring 2002Notes 0914 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

CS 277 – Spring 2002Notes 0915 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

CS 277 – Spring 2002Notes 0916 So net effect is either S=…r 1 (x)…w 2 (b)… or S=…w 2 (B)…r 1 (x)…

CS 277 – Spring 2002Notes 0917 Assume equivalent to either r 1 (A) w 2 (A) orw 2 (A) r 1 (A)  low level synchronization mechanism Assumption 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

CS 277 – Spring 2002Notes 0918 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.

CS 277 – Spring 2002Notes 0919 Definition A schedule is conflict serializable if it is conflict equivalent to some serial schedule.

CS 277 – Spring 2002Notes 0920 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 )

CS 277 – Spring 2002Notes 0921 Exercise: What 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) Is S serializable?

CS 277 – Spring 2002Notes 0922 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

CS 277 – Spring 2002Notes 0923 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)

CS 277 – Spring 2002Notes 0924 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

CS 277 – Spring 2002Notes 0925 (  ) 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

CS 277 – Spring 2002Notes 0926 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

CS 277 – Spring 2002Notes 0927 Option 2: prevent P(S) cycles from occurring T 1 T 2 …..T n Scheduler DB How to enforce serializable schedules?

CS 277 – Spring 2002Notes 0928 A locking protocol Two new actions: lock (exclusive):l i (A) unlock:u i (A) scheduler T 1 T 2 lock table

CS 277 – Spring 2002Notes 0929 Rule #1: Well-formed transactions T i : … l i (A) … p i (A) … u i (A)...

CS 277 – Spring 2002Notes 0930 Rule #2 Legal scheduler S = …….. l i (A) ………... u i (A) ……... no l j (A)

CS 277 – Spring 2002Notes 0931 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:

CS 277 – Spring 2002Notes 0932 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:

CS 277 – Spring 2002Notes 0933 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)

CS 277 – Spring 2002Notes 0934 Schedule F T1 T 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) A B

CS 277 – Spring 2002Notes 0935 Rule #3 Two phase locking (2PL) for transactions T i = ……. l i (A) ………... u i (A) ……... no unlocks no locks

CS 277 – Spring 2002Notes 0936 # locks held by Ti Time Growing Shrinking Phase Phase

CS 277 – Spring 2002Notes 0937 Schedule G delayed

CS 277 – Spring 2002Notes 0938 Schedule G delayed

CS 277 – Spring 2002Notes 0939 Schedule G delayed

CS 277 – Spring 2002Notes 0940 Schedule H (T 2 reversed) delayed

CS 277 – Spring 2002Notes 0941 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!

CS 277 – Spring 2002Notes 0942 Next step: Show that rules #1,2,3  conflict- serializable schedules

CS 277 – Spring 2002Notes 0943 Conflict rules for l i (A), u i (A): l i (A), l j (A) conflict l i (A), u j (A) conflict Note: no conflict,,...

CS 277 – Spring 2002Notes 0944 Theorem Rules #1,2,3  conflict (2PL) serializable schedule To help in proof: Definition Shrink(Ti) = SH(Ti) = first unlock action of Ti

CS 277 – Spring 2002Notes 0945 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)

CS 277 – Spring 2002Notes 0946 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

CS 277 – Spring 2002Notes 0947 Beyond this simple 2PL protocol, it is all a matter of improving performance and allowing more concurrency…. –Shared locks –Multiple granularity –Inserts, deletes and phantoms –Other types of C.C. mechanisms

CS 277 – Spring 2002Notes 0948 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)

CS 277 – Spring 2002Notes 0949 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

CS 277 – Spring 2002Notes 0950 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) …

CS 277 – Spring 2002Notes 0951 What 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) …

CS 277 – Spring 2002Notes 0952 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?

CS 277 – Spring 2002Notes 0953 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)

CS 277 – Spring 2002Notes 0954 A way to summarize Rule #2 Compatibility matrix Comp S X S true false Xfalse false

CS 277 – Spring 2002Notes 0955 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

CS 277 – Spring 2002Notes 0956 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

CS 277 – Spring 2002Notes 0957 Lock types beyond S/X Examples: (1) increment lock (2) update lock

CS 277 – Spring 2002Notes 0958 Example (1): increment lock Atomic increment action: IN i (A) {Read(A); A  A+k; Write(A)} IN 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)

CS 277 – Spring 2002Notes 0959 CompSXI S X I

CS 277 – Spring 2002Notes 0960 CompSXI STFF XFFF IFFT

CS 277 – Spring 2002Notes 0961 Update locks

CS 277 – Spring 2002Notes 0962 Solution If T i wants to read A and knows it may later want to write A, it requests update lock (not shared)

CS 277 – Spring 2002Notes 0963 CompSXU S X U New request Lock already held in

CS 277 – Spring 2002Notes 0964 CompSXU STFT XFFF U TorF FF -> symmetric table? New request Lock already held in

CS 277 – Spring 2002Notes 0965 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)…? To grant a lock in mode t, mode t must be compatible with all currently held locks on object

CS 277 – Spring 2002Notes 0966 How does locking work in practice? Every system is different (E.g., may not even provide CONFLICT-SERIALIZABLE schedules) But here is one (simplified) way...

CS 277 – Spring 2002Notes 0967 (1) Don’t trust transactions to request/release locks (2) Hold all locks until transaction commits # locks time Sample Locking System:

CS 277 – Spring 2002Notes 0968 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

CS 277 – Spring 2002Notes 0969 Lock table Conceptually A  B C ... Lock info for B Lock info for C If null, object is unlocked Every possible object

CS 277 – Spring 2002Notes 0970 But use hash table: A If object not found in hash table, it is unlocked Lock info for A A... H

CS 277 – Spring 2002Notes 0971 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

CS 277 – Spring 2002Notes 0972 What are the objects we lock? ? Relation A Relation B... Tuple A Tuple B Tuple C... Disk block A Disk block B... DB

CS 277 – Spring 2002Notes 0973 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

CS 277 – Spring 2002Notes 0974 We can have it both ways!! Ask any janitor to give you the solution... hall Stall 1Stall 2Stall 3Stall 4 restroom

CS 277 – Spring 2002Notes 0975 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (S)

CS 277 – Spring 2002Notes 0976 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (IX) T 2 (IX)

CS 277 – Spring 2002Notes 0977 Multiple granularity CompRequestor IS IX S SIX X IS Holder IX S SIX X

CS 277 – Spring 2002Notes 0978 Multiple granularity CompRequestor IS IX S SIX X IS Holder IX S SIX X TTTTF F F F FFFFF FFFT FTFT FFTT

CS 277 – Spring 2002Notes 0979 ParentChild can belocked in IS IX S SIX X P C

CS 277 – Spring 2002Notes 0980 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

CS 277 – Spring 2002Notes 0981 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

CS 277 – Spring 2002Notes 0982 Exercise: Can 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)

CS 277 – Spring 2002Notes 0983 Exercise: Can 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)

CS 277 – Spring 2002Notes 0984 Exercise: Can 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)

CS 277 – Spring 2002Notes 0985 Exercise: Can 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)

CS 277 – Spring 2002Notes 0986 Exercise: Can 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)

CS 277 – Spring 2002Notes 0987 Insert + delete operations Insert A Z ...

CS 277 – Spring 2002Notes 0988 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

CS 277 – Spring 2002Notes 0989 Phantoms Still have a problem: Phantoms Example: relation R (E#,name,…) constraint: E# is key use tuple locking RE#Name…. o155Smith o275Jones

CS 277 – Spring 2002Notes 0990 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 [99,Gore,..] Insert o 4 [99,Bush,..]...

CS 277 – Spring 2002Notes 0991 Solution Use multiple granularity tree Before insert of node Q, lock parent(Q) in X mode R1 t1t1 t2t2 t3t3

CS 277 – Spring 2002Notes 0992 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# = 99 already in R! delayed

CS 277 – Spring 2002Notes 0993 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...

CS 277 – Spring 2002Notes 0994 This approach can be generalized to multiple indexes...

CS 277 – Spring 2002Notes 0995 Next: Tree-based concurrency control Validation concurrency control

CS 277 – Spring 2002Notes 0996 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??

CS 277 – Spring 2002Notes 0997 Idea: traverse like “Monkey Bars” A B C D EF T 1 lock

CS 277 – Spring 2002Notes 0998 Why does this work? Assume all T i start at root; exclusive lock T i  T j  T i locks root before T j Actually works if we don’t always start at root Root Q T i  T j

CS 277 – Spring 2002Notes 0999 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

CS 277 – Spring 2002Notes Tree-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

CS 277 – Spring 2002Notes Validation Transactions have 3 phases: (1) Read –all DB values read –writes to temporary storage –no locking (2) Validate –check if schedule so far is serializable (3) Write –if validate ok, write to DB

CS 277 – Spring 2002Notes Key idea Make validation atomic If 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...

CS 277 – Spring 2002Notes To implement validation, system keeps two sets: FIN = transactions that have finished phase 3 (and are all done) VAL = transactions that have successfully finished phase 2 (validation)

CS 277 – Spring 2002Notes 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  = 

CS 277 – Spring 2002Notes 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

CS 277 – Spring 2002Notes 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)

CS 277 – Spring 2002Notes 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

CS 277 – Spring 2002Notes 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 } ]

CS 277 – Spring 2002Notes 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 ?

CS 277 – Spring 2002Notes 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;

CS 277 – Spring 2002Notes 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

CS 277 – Spring 2002Notes Validation (also called optimistic concurrency control) is useful in some cases: - Conflicts rare - System resources plentiful - Have real time constraints

CS 277 – Spring 2002Notes Summary Have studied C.C. mechanisms used in practice - 2 PL - Multiple granularity - Tree (index) protocols - Validation