DBMS 2001Notes 8: Concurrency1 Principles of Database Management Systems 8: Concurrency Control Pekka Kilpeläinen (after Stanford CS245 slide originals.

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.
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.
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.
©Silberschatz, Korth and Sudarshan16.1Database System Concepts 3 rd Edition Chapter 16: Concurrency Control Lock-Based Protocols Timestamp-Based Protocols.
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)
Concurrency. Correctness Principle A transaction is atomic -- all or none property. If it executes partly, an invalid state is likely to result. A transaction,
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.
©Silberschatz, Korth and Sudarshan16.1Database System Concepts 3 rd Edition Chapter 16: Concurrency Control Lock-Based Protocols Timestamp-Based Protocols.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #21 Concurrency Control Professor Elke A. Rundensteiner.
CS 277 – Spring 2002Notes 091 CS 277: Database System Implementation Notes 09: Concurrency Control Arthur Keller.
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.
DBMS2001Notes 9: Transaction Processing1 Principles of Database Management Systems 9: More on Transaction Processing Pekka Kilpeläinen (Partially based.
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.
Introduction to Data Management CSE 344 Lecture 23: Transactions CSE Winter
1 CSE232A: Database System Principle Concurrency Control.
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.
Concurrency control In production environments, it is unlikely that we can limit our system to just one user at a time. – Consequently, it is possible.
Concurrency Control Concurrency Control By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner.
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
1 Database Systems ( 資料庫系統 ) December 27, 2004 Chapter 17 By Hao-hua Chu ( 朱浩華 )
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.
CS 540 Database Management Systems Concurrency Control 1.
CS 440 Database Management Systems
Concurrency Control.
Database Systems II Concurrency Control
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
Chapter 15 : Concurrency Control
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:

DBMS 2001Notes 8: Concurrency1 Principles of Database Management Systems 8: Concurrency Control Pekka Kilpeläinen (after Stanford CS245 slide originals by Hector Garcia-Molina, Jeff Ullman and Jennifer Widom)

DBMS 2001Notes 8: Concurrency2 Chapter 9Concurrency Control T1 T2… Tn DB (consistency constraints) How to prevent harmful interference btw transactions? => scheduling techniques based on - locks - timestamps and validation

DBMS 2001Notes 8: Concurrency3 Example: T1:Read(A)T2:Read(A) A  A+100A  A  2Write(A)Read(B) B  B+100B  B  2Write(B) Constraint: A=B

DBMS 2001Notes 8: Concurrency4 Correctness depends on scheduling of transactions A schedule - Chronological (possibly interleaving) order in which actions of transactions are executed - A correct schedule is equivalent to executing transactions one-at-a-time in some order

DBMS 2001Notes 8: Concurrency5 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 OK

DBMS 2001Notes 8: Concurrency6 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 OK

DBMS 2001Notes 8: Concurrency7 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 OK

DBMS 2001Notes 8: Concurrency8 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 Constraint violation!

DBMS 2001Notes 8: Concurrency9 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’ OK

DBMS 2001Notes 8: Concurrency10 Want schedules that are “good”,regardless of –initial state (  “good” in any DB state) and –transaction semantics Only look at order of READs and WRITEs –Note: transactions see values in buffers, not on disk => this time ignore INPUT/OUTPUTs Example: Sa (Schedule a) = r1(A)w1(A)r1(B)w1(B) r2(A)w2(A) r2(B)w2(B) T1 T2

DBMS 2001Notes 8: Concurrency11 A schedule is serial, if actions of transactions are not interleaved –e.g., (T1, T2) or (T2, T1) –A serial schedule obviously maintains consistency (assuming correctness of individual transactions) Could we reorder a schedule into an equivalent serial schedule? –Actions conflict, if swapping them may change the meaning of a schedule: any two actions of a single transaction two actions on a common DB element A, one of which is WRITE(A)

DBMS 2001Notes 8: Concurrency12 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) r 1 (B) w 2 (A) r 1 (B)r 2 (A) w 1 (B)w 2 (A) (of swapping non-conflicting actions)

DBMS 2001Notes 8: Concurrency13 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) Sd cannot be rearranged into a serial schedule Sd is not “equivalent” to any serial schedule Sd is “bad”

DBMS 2001Notes 8: Concurrency14 Concepts Transaction: sequence of r i (x), w i (x) actions Conflicting actions: r h (A) w h (A) w h (A) w k (A) r k (A) w k (A) If schedule S contains conflicting actions …, p h (A), …, q k (A),... [i.e., one of p, q is w], transaction T h must precede T k in a corresponding serial schedule. Denote this by T h  T k

DBMS 2001Notes 8: Concurrency15 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 )

DBMS 2001Notes 8: Concurrency16 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. (=> effect of both S1 and S2 on the DB is the same)

DBMS 2001Notes 8: Concurrency17 Definition A schedule is conflict serializable if it is conflict equivalent to some serial schedule. NB: Conflict serializability is a sufficient (but not a necessary) condition for serializability (equivalence to some serial schedule) Easier to enforce than serializability, therefore generally assured by commercial systems

DBMS 2001Notes 8: Concurrency18 Nodes: transactions T1, T2,... in S Arcs: Ti  Tj for i  j whenever - pi(A), qj(A) are conflicting actions in S, (same element A, at least one of actions is a write) - action pi(A) precedes qj(A) in S Precedence graph P(S) (S is schedule )

DBMS 2001Notes 8: Concurrency19 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?

DBMS 2001Notes 8: Concurrency20 Lemma Let S 1, S 2 be schedules for the same set of transactions 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 P(S 1 ) and not in P(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

DBMS 2001Notes 8: Concurrency21 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)

DBMS 2001Notes 8: Concurrency22 Theorem I P(S 1 ) acyclic  S 1 conflict serializable (  ) Assume S 1 is conflict serializable   serial S s : S s, S 1 conflict equivalent  P(S s ) = P(S 1 ) [  Lemma]  P(S 1 ) acyclic since P(S s ) is acyclic

DBMS 2001Notes 8: Concurrency23 (  ) Assume P(S 1 ) is acyclic Transform S 1 as follows: (1) Take T 1 to be transaction with no incoming arcs (2) Move all T 1 actions to the front S 1 = …… q j (X) …… p 1 (A) … (3) we now have S 1 = (4) repeat above steps to serialize rest! Theorem (cont.) P(S 1 ) acyclic  S 1 conflict serializable T1T1 T3T3 T2T2 T4T4

DBMS 2001Notes 8: Concurrency24 How to enforce serializable schedules? Option 1: (Optimistic strategy) Run system, recording P(S); At end of day, check P(S) for cycles, and declare if execution was good

DBMS 2001Notes 8: Concurrency25 Option 2: (Pessimistic strategy) Prevent occurrence of cycles in P(S) T 1 T 2 …..T n Scheduler DB How to enforce serializable schedules?

DBMS 2001Notes 8: Concurrency26 A locking protocol Two new actions: lock (exclusive):l j (A) unlock:u j (A) scheduler T j T i lock table

DBMS 2001Notes 8: Concurrency27 Rule #1: Well-formed transactions T i : … l i (A) … p i (A) … u i (A)... Lock elements (A) before accessing them (pi is a read or a write) Eventually, release the locks (ui(A))

DBMS 2001Notes 8: Concurrency28 Rule #2 Legal scheduler S = …….. l i (A) ………... u i (A) ……... no l j (A) for i  j At most one transaction T i can hold a lock on any element A

DBMS 2001Notes 8: Concurrency29 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) Exercise: 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)

DBMS 2001Notes 8: Concurrency30 Schedule F (with simple 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) Constraint violation! A B

DBMS 2001Notes 8: Concurrency31 Simple-minded locking not sufficient to ensure serializability (i.e., correctness)!  More advanced protocol known as "two phase locking"

DBMS 2001Notes 8: Concurrency32 Rule #3 Two phase locking (2PL) for transactions T i = ……. l i (A) ………... u i (A) ……... no unlocks no locks All lock requests of a transaction have to precede its unlock requests

DBMS 2001Notes 8: Concurrency33 # locks held by Ti Time Growing Shrinking Phase Phase

DBMS 2001Notes 8: Concurrency34 Schedule G (with 2PL) delayed

DBMS 2001Notes 8: Concurrency35 Schedule H (T 2 reversed) delayed Neither proceeds: a deadlock –System must rollback (= abort & restart) at least one of T1, T2

DBMS 2001Notes 8: Concurrency36 Show that Rules #1,2,3  conflict (2PL) serializable schedule Next step:

DBMS 2001Notes 8: Concurrency37 # locks held by Ti To help in proof: Definition Shrink(Ti) = SH(Ti) = first unlock action of Ti T1 T2 Time SH(T2) SH(T1)

DBMS 2001Notes 8: Concurrency38 Lemma Let S be a 2PL schedule. Ti  Tj in P(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)

DBMS 2001Notes 8: Concurrency39 Proof: Let S be a 2PL schedule. Assume P(S) has cycle T 1  T 2  …. T n  T 1 By Lemma: SH(T 1 ) < SH(T 2 ) <... < SH(T 1 ) Impossible, so P(S) acyclic  S is conflict serializable (by Th. I) Theorem II Rules #1,2,3 conflict (that is, 2PL)  serializable schedule

DBMS 2001Notes 8: Concurrency40 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 Timestamping Validation

DBMS 2001Notes 8: Concurrency41 Shared locks So far only exclusive locks: S =...l 1 (A) r 1 (A) u 1 (A) … l 2 (A) r 2 (A) u 2 (A) … Do not conflict  locking unnecessary Instead, use shared locks (S) for reading: S=... ls1(A) r1(A) ls2(A) r2(A) …. u1(A)u2(A)

DBMS 2001Notes 8: Concurrency42 Write actions conflict  use exclusive (X) locks for writing Lock actions: l-m k (A): lock A in mode m (S or X) for T k u k (A): release (whatever) lock(s) held by transaction T k on element A

DBMS 2001Notes 8: Concurrency43 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) … Request –an S-lock for reading –an X-lock for writing Release the locks eventually

DBMS 2001Notes 8: Concurrency44 What about transactions that first read and later write the same element? Option 1: Request exclusive lock T i =...l-X 1 (A) … r 1 (A)... w 1 (A)... u 1 (A) … 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 as getting 2nd lock on A

DBMS 2001Notes 8: Concurrency45 Rule #2 Legal scheduler S =....l-S i (A) … … u i (A) … no l-X j (A) for j  i S =... l-X i (A) … … u i (A) … no l-X j (A) for j  i no l-S j (A) for j  i

DBMS 2001Notes 8: Concurrency46 A way to summarize Rule #2 Compatibility matrix: S X S true false Xfalse false Locks already held by some Ti Lock requested by some Tj True OK to give a new lock of requested kind

DBMS 2001Notes 8: Concurrency47 Rule # 3 (2PL) Only change to previous: Lock upgrades S(A)  {S(A), X(A)} or S(A)  X(A) are allowed only in the growing phase

DBMS 2001Notes 8: Concurrency48 Proof: Similar to the X locks case Theorem Rules 1,2,3  Conf.serializable for S/X locks schedules Lock types beyond S/X Examples: (1) update lock (2) increment lock (see the textbook)

DBMS 2001Notes 8: Concurrency49 Update locks

DBMS 2001Notes 8: Concurrency50 Solution If T i wants to read A and knows it may later want to write A, it requests an update lock (not shared)

DBMS 2001Notes 8: Concurrency51 CompSXU STFT XFFF U FFF New request Lock already held in As before

DBMS 2001Notes 8: Concurrency52 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)… To grant a lock in mode m, mode m must be compatible with all currently held locks on object

DBMS 2001Notes 8: Concurrency53 How does locking work in practice? Every system is different (E.g., may not even provide CONFLICT-SERIALIZABLE schedules) Here is one (simplified) way...

DBMS 2001Notes 8: Concurrency54 (1) Don’t trust transactions to request/release locks (2) Do not release locks until transaction commits/aborts: # locks time Sample Locking System:

DBMS 2001Notes 8: Concurrency55 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 Insert appropriate lock requests Execute or delay, based on existing locks

DBMS 2001Notes 8: Concurrency56 Lock table Conceptually A  B C ... Lock info for B Lock info for C If null, object is unlocked Every possible object

DBMS 2001Notes 8: Concurrency57 But use hash table: A If object not found in hash table, it is unlocked Lock info for A A... h

DBMS 2001Notes 8: Concurrency58 Lock info for A - example tran mode wait? Nxt T_link Element: A Group mode: U Waiting: yes List: T1 S no T2 U no T3X yes  To other lock table entries of transaction T3

DBMS 2001Notes 8: Concurrency59 What are the objects we lock? ? Relation A Relation B... Tuple A Tuple B Tuple C... Disk block A Disk block B... DB

DBMS 2001Notes 8: Concurrency60 Locking works in any case, but should we choose small or large objects? If we lock large objects (e.g., Relations) –Need few locks –Get low concurrency If we lock small objects (e.g., tuples,fields) –Need more locks (=> overhead higher) –Get more concurrency

DBMS 2001Notes 8: Concurrency61 We can have it both ways!! Ask any janitor to give you the solution... hall Stall 1Stall 2Stall 3Stall 4 restroom

DBMS 2001Notes 8: Concurrency62 Warning Protocol Hierarchically nesting elements (e.g. relation/block/tuple) can be locked with intention locks IS and IX Idea –start locking at the root (relation) level –to place an S or X lock on a subelement, first place a corresponding intention lock IS or IX the element itself Warns others: "I'll be reading/writing some subelement of this element"

DBMS 2001Notes 8: Concurrency63 Example (T 1 reads t 2, T 2 reads R1) R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (S)

DBMS 2001Notes 8: Concurrency64 Example (T 1 reads t 2, T 2 writes to t 4 ) R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (IX) T 2 (X)

DBMS 2001Notes 8: Concurrency65 Compatibility of multiple granularity locks Comp Requestor IS IX S X IS Holder IX S X TTTF FFFF FTFT FFTT As before

DBMS 2001Notes 8: Concurrency66 ParentChild can belocked in IS IX S X P C IS, S IS, S, IX, X [S, IS; not necessary] none

DBMS 2001Notes 8: Concurrency67 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) can be locked by Ti in IX or IS (4) Node Q can be locked by Ti in X,IX only if parent(Q) locked by Ti in IX (5) Ti is two-phase (6) Ti can unlock node Q only if none of Q’s children are locked by Ti

DBMS 2001Notes 8: Concurrency68 Exercise: Can T 2 write element f 2.2 ? 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)

DBMS 2001Notes 8: Concurrency69 Exercise: Can T 2 write element f 2.2 ? 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)

DBMS 2001Notes 8: Concurrency70 Exercise: Can T 2 write element f 3.1 ? 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)

DBMS 2001Notes 8: Concurrency71 Exercise: Can T 2 read element f 2.2 ? 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 (S,IX) T 1 (X)

DBMS 2001Notes 8: Concurrency72 Exercise: Can T 2 write element f 2.2 ? 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 (S,IX) T 1 (X)

DBMS 2001Notes 8: Concurrency73 Deletions similar to writes: Get an exclusive lock on A before deleting A Insertions more problematic: –possible to lock only existing elements Phantom tuples: –tuples that should have been locked, but did not exist when the locks were taken

DBMS 2001Notes 8: Concurrency74 Phantom tuples Example: relation R (E#,name,…) constraint: E# is key use tuple locking RE#NameFInit …. t155Bush G t275ClintonW

DBMS 2001Notes 8: Concurrency75 T 1 : Insert into R T 2 : Insert into R T 1 T 2 l-S 1 (t 1 ) l-S 2 (t 1 ) l- S 1 (t 2 ) l-S 2 (t 2 ) Check Constraint Insert [99,Gore,A,..] Insert [99,Bush,G,..]...

DBMS 2001Notes 8: Concurrency76 Solution Use multiple granularity tree Before insert of node Q, lock parent(Q) in X mode R1 t1t1 t2t2 t3t3

DBMS 2001Notes 8: Concurrency77 Back to example T 1 : Insert T 2 : Insert T 1 T 2 l-X 1 (R) Check constraint Insert u 1 (R) l-X 2 (R) Check constraint Oops! e# = 99 already in R! delayed

DBMS 2001Notes 8: Concurrency78 Validation Lock-based concurrency control is pessimistic: non-serializable schedules are prevented in advance Another, optimistic strategy: –allow transaction Ti access data without locks, but record elements read or written by Ti (in read and write sets RS(Ti) and WS(Ti)) –at the end validate that the actions correspond to some serial schedule

DBMS 2001Notes 8: Concurrency79 Validation Transactions have 3 phases: (1) Read –all accessed DB elements read –writes to temporary storage (no locking) (2) Validate –check if schedule so far is serializable; if yes, then... (3) Write –write updated elements to DB

DBMS 2001Notes 8: Concurrency80 Key idea Make validation an atomic operation –i.e., validate a single transaction at a time 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...

DBMS 2001Notes 8: Concurrency81 To implement validation, system maintains two sets of transactions: FIN = transactions that have finished phase 3 (writing, and are completed) VAL = transactions that have successfully finished phase 2 (validation), but not yet completed

DBMS 2001Notes 8: Concurrency82 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 validating T 3 start     T2 may have written B after T3 read B, contradicting the assumed serial order (T2, T3)  T3 is rolled back

DBMS 2001Notes 8: Concurrency83 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    

DBMS 2001Notes 8: Concurrency84 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 validating finish T 2 BAD: w 3 (D) w 2 (D)    

DBMS 2001Notes 8: Concurrency85 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    

DBMS 2001Notes 8: Concurrency86 Validation rules for T j : (1) When Tj starts phase 1 (reading DB): Ignore(Tj)  FIN; // Transactions that // do not affect the validation of Tj (2) At Validation of Tj: if Validates(Tj) then VAL  VAL U {Tj}; do the write phase; FIN  FIN U {Tj}; VAL  VAL - {Tj};

DBMS 2001Notes 8: Concurrency87 Validates(T j ): // returns True if T j validates for each U  VAL do if ( WS(U)  RS(Tj)   or WS(U)  WS(Tj)   ) then return False; end for; for each U  FIN - Ignore(Tj) do if ( WS(U)  RS(Tj)   ) then return False; end for; return True;

DBMS 2001Notes 8: Concurrency88 Exercise: Validation of U, T, V and W 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

DBMS 2001Notes 8: Concurrency89 Validation is useful in some cases: - If interaction among transactions low  rollbacks rare - If system resources are plentiful - slightly more bookkeeping than for locking - If there are real-time constraints - causes no delays for transactions

DBMS 2001Notes 8: Concurrency90 Summary Have studied C.C. mechanisms used in practice - 2 PL - Multiple granularity - Validation