Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Advertisements

Database Systems (資料庫系統)
Concurrency Control WXES 2103 Database. Content Concurrency Problems Concurrency Control Concurrency Control Approaches.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Transaction Management and Concurrency Control
Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
1 Concurrency Control Chapter Conflict Serializable Schedules  Two actions are in conflict if  they operate on the same DB item,  they belong.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Introduction to Database Systems1 Concurrency Control CC.Lecture 1.
Principles of Transaction Management. Outline Transaction concepts & protocols Performance impact of concurrency control Performance tuning.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner.
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Sections
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. –Because disk accesses are.
Quick Review of Apr 29 material
ICS 421 Spring 2010 Transactions & Concurrency Control (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Concurrency Control R &G - Chapter 19 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
1 Concurrency Control and Recovery Module 6, Lecture 1.
1 Transaction Management Overview Yanlei Diao UMass Amherst March 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Transaction Processing: Concurrency and Serializability 10/4/05.
Transaction Management
Concurrency Control R &G - Chapter 19 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
1 Concurrency Control. 2 Transactions A transaction is a list of actions. The actions are reads (written R T (O)) and writes (written W T (O)) of database.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Lecture 21 Ramakrishnan - Chapter 18.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Concurrency Control R &G - Chapter 19. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses.
Database Systems/COMP4910/Spring05/Melikyan1 Transaction Management Overview Unit 2 Chapter 16.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Concurrency Server accesses data on behalf of client – series of operations is a transaction – transactions are atomic Several clients may invoke transactions.
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
1 Concurrency Control II: Locking and Isolation Levels.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu.
1 Concurrency Control Chapter Conflict Serializable Schedules  Two schedules are conflict equivalent if:  Involve the same actions of the same.
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
1 Database Systems ( 資料庫系統 ) December 27, 2004 Chapter 17 By Hao-hua Chu ( 朱浩華 )
Transaction Management and Recovery, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
1 Database Systems ( 資料庫系統 ) December 27/28, 2006 Lecture 13 Merry Christmas & New Year.
MULTIUSER DATABASES : Concurrency and Transaction Management.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
Database Systems (資料庫系統)
Transaction Management Overview
Database Systems (資料庫系統)
Concurrency Control.
Transaction Management Overview
Transaction Management
Transaction Management Overview
Transaction Management
Transaction Management Overview
Concurrency Control Chapter 17
Lecture 21: Concurrency & Locking
CS162 Operating Systems and Systems Programming Review (II)
Chapter 15 : Concurrency Control
Concurrency Control Chapter 17
Lecture 22: Intro to Transactions & Logging IV
Transaction management
Transaction Management
Transaction Management Overview
Database Systems (資料庫系統)
Database Systems (資料庫系統)
Transaction Management Overview
Presentation transcript:

Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke

Modeling Transactions  User programs may carry out many operations …  Data modification and retrieval  Prompting user for input  … but the DBMS is only concerned about what data is read/written from/to the database  A transaction is abstracted by a sequence of time-ordered read and write actions  e.g., R(X), R(Y), W(X), W(Y)  R=read, W=write, data element in parentheses  Each individual action is indivisible, or atomic  SQL UPDATE = R(X) W(X) 2

 Consider two transactions (in a really bad DB) where A = 100  T1 & T2 are concurrent, running same transaction program  T1& T2 both read old value, 100, add 100, store 200  One of the updates has been lost!  Consistency requirement: after execution, A should reflect all deposits (Money should not be created or destroyed)  No guarantee that T1 will execute before T2 or vice-versa…  … but the net effect must be equivalent to these two transactions running one-after-the-other in some order Concurrency: lost update anomaly T1:A = A T2:A = A

 Consider two transactions running different programs  T1 performs an account transfer  T2 performs credit of (6%) interest amount  Consistency requirement: after execution, sum of accounts must be 106% the initial sum (before execution)  No guarantee that T1 will execute before T2 or vice-versa…  … but the net effect must be equivalent to these two transactions running one-after-the-other in some order Concurrency: more complex case (1/3) T1:A=A+100, B=B-100 T2:A=1.06*A, B=1.06*B 4

 After execution, A=636, B=424, A+B=1060 Concurrency: when things go wrong (2/3)  Assume that initially there are $500 in both accounts  Consider a possible interleaving or schedule T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B CORRECT 5

Concurrency: when things go wrong (3/3)  Consider another interleaving or schedule: T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B  After execution, A=636, B=430, A+B=1066 WRONG!!! T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B)  The DBMS view 6

 What if one of the operations is a WRITE? Concurrent Execution Anomalies  Anomalies may occur in concurrent execution  The notion of conflicts helps understand anomalies  Is there a conflict when multiple READ operations are posted? No YES! 7  WR, RW and WW conflicts

WR Conflicts  Reading Uncommitted Data (Dirty Reads)  The earlier example where interest is not properly credited is due to a WR conflict  Value of A written by T1 is read by T2 before T1 completed all its changes 8 T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B)

RW Conflicts T1:R(A), R(A), W(A), Commit T2:R(A), W(A), Commit  Unrepeatable Reads  Scenario: Let A (=1) be the number of copies of an item. T1 checks the number available. If the number is greater than 0, T1 places an order by decrementing the count  In the meantime, T2 updated the value of the count (say, to zero)  T1 will set the count to a negative value! 9

WW Conflicts  Overwriting Uncommitted Data  Assume two employees must always have same salary  T1 sets the salaries to $1000, T2 to $2000  There is a “lost update”, and the final salaries are $1000 and $2000  “Lost” update because the transaction that comes last in serial order should set both values. One got lost. T1:W(A), W(B), Commit T2:W(A), W(B), Commit 10

Scheduling Transactions  Preventing anomalies: only allow some schedules to occur  Disallow “unsafe” interleavings  Serial schedule: no interleaving of transactions  Safe, but poor performance!  Schedule equivalence: two schedules are equivalent if they lead to the same state of the DBMS (see footnote on pg. 525 that includes values returned to user in relevant ”state”)  Serializable schedule: schedule that is equivalent to some serial execution of transactions  But still allows interleaving/concurrency! 11

 Same effect as executing T1 completely, then T2 Serializable schedule example T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B 12

Conflict Serializable Schedules  Two schedules are conflict equivalent if:  Involve the same actions of the same transactions  Every pair of conflicting actions is ordered the same way  Schedule S is conflict serializable if S is conflict equivalent to some serial schedule

Dependency Graph  Dependency graph:  one node per transaction  edge from Ti to Tj if action of Ti precedes and conflicts with action of Tj  Theorem: Schedule is conflict serializable if and only if its dependency graph is acyclic  Equivalent serial schedule given by topological sort of dependency graph

Example  A schedule that is not conflict serializable:  The cycle in the graph reveals the problem. The output of T1 depends on T2, and vice-versa. T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B) T1T2 A B Dependency graph

Why is serializability important?  If each transaction preserves consistency, every serializable schedule preserves consistency  How to ensure serializable schedules?  Use locking protocols  DBMS inserts proper locking actions, user is oblivious to locking (except through its effect on performance, and deadlocks) 16

Two-Phase Locking (2PL)  Two-Phase Locking Protocol  Each transaction must obtain  S (shared) lock on object before reading  X (exclusive) lock on object before writing  A transaction can not request additional locks once it releases any locks.

Strict 2PL  Strict Two-phase Locking (Strict 2PL) Protocol:  Each transaction must obtain  S (shared) lock on object before reading,  X (exclusive) lock on object before writing  All locks are released when the transaction completes  Strict 2PL allows only schedules whose precedence graph is acyclic  Hence conflict serializable schedules  Equivalent serial schedule obtained by topological sort

Strict Two-Phase Locking (Strict 2PL)  Protocol steps  Each transaction must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing.  All locks held are released when the transaction completes  (Non-strict) 2PL: Release locks anytime, but cannot acquire locks after releasing any lock.  Strict 2PL allows only serializable schedules.  It simplifies transaction aborts  (Non-strict) 2PL also allows only serializable schedules, but involves more complex abort processing 19

Strict 2PL Example (red op is blocked) 20 T1: S(A) R(A) T2: S(A) R(A) X(B) S(B) T1: S(A) R(A) T2: S(A) R(A) X(B) R(B) S(B) T1: S(A) R(A) T2: S(A) R(A) X(B) R(B) S(B) W(B) C T1: S(A) R(A) T2: S(A) R(A) X(B) R(B) S(B) R(B) C W(B) C

Aborting Transactions  When Ti is aborted, all its actions have to be undone  if Tj reads an object last written by Ti, Tj must be aborted as well!  cascading aborts can be avoided by releasing locks only at commit  If Ti writes an object, Tj can read this only after Ti commits  In Strict 2PL, cascading aborts are prevented  At the cost of decreased concurrency  No free lunch!  Increased parallelism leads to locking protocol complexity 21

View Serializability (1/3)  Conflict serializability is sufficient, but not necessary!  View serializability is a more general sufficient condition  Schedules S1 and S2 are view equivalent if:  If Ti reads initial value of A in S1, then Ti also reads initial value of A in S2  If Ti reads value of A written by Tj in S1, then Ti also reads value of A written by Tj in S2  If Ti writes final value of A in S1, then Ti also writes final value of A in S2  Same “reads-from” graph

View Serializability (2/3)  An Example T1: R(A) W(A) T2: W(A) T3: W(A) T1: R(A),W(A) T2: W(A) T3: W(A) Non-conflict serializable Equivalent serial schedule

View Serializability (3/3)  A schedule is view serializable if it is view equivalent to a serial schedule.  Every conflict serializable schedule is view serializable.  The converse is not true.  Every view serializable schedule that is not conflict serializable contains a blind write.  Unfortunately, testing is not practical  Only of theoretical interest

Deadlocks  Cycle of transactions waiting for locks to be released by each other  Two ways of dealing with deadlocks:  Deadlock prevention  Deadlock detection T1: X(A) W(A) S(B) [R(B) …] T2:X(B) W(B) S(A) [R(A) …] 25

Deadlock Detection  Create a waits-for graph:  Nodes are transactions  Edge from Ti to Tj if Ti is waiting for Tj to release a lock T1: S(A), R(A), S(B) T2: X(B),W(B) X(C) T3: S(C), R(C) T4: X(B) T1T2 T4T3 26 X(A)

Deadlock Prevention  Assign priorities based on timestamps  Assume Ti wants a lock that Tj holds  Wait-Die: It Ti > Tj, Ti waits for Tj; otherwise Ti aborts  Wound-wait: If Ti > Tj, Tj aborts; otherwise Ti waits  Fairness is an issue  If transaction re-starts, make sure it has its original timestamp  Otherwise starvation may occur 27

Locking Performance  Lock-based schemes rely on two mechanisms  Blocking  Aborting  Both blocking and aborting cause performance overhead  Transactions may have to wait  Transactions may need to be re-executed  How does blocking affect throughput?  First few transactions do not conflict – no blocking  Parallel execution, performance increase  As more transactions execute, blocking occurs  After a point, adding more transactions decreases throughput!

Locking Performance (2) Active Transaction Count Thrashing

Improving Performance  Locking the smallest-sized objects possible  e.g., row set instead of table  Reduce the time a lock is held for  Release locks faster  But careful not to cause too many cascaded aborts  Reducing hot spots  Careful review of application design  Reduce contention

Lock Management  Lock and unlock requests are handled by the lock manager  Lock table entry:  Number of transactions currently holding a lock  Type of lock held (shared or exclusive)  Pointer to queue of lock requests  Locking and unlocking have to be atomic operations