CS4432: Database Systems II Transaction Management Motivation 1.

Slides:



Advertisements
Similar presentations
Cs4432concurrency control1 CS4432: Database Systems II Lecture #21 Concurrency Control : Theory Professor Elke A. Rundensteiner.
Advertisements

Introduction to Database Systems1 Concurrency Control CC.Lecture 1.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Chapter 15: Transactions Transaction Concept Transaction Concept Concurrent Executions Concurrent Executions Serializability Serializability Testing for.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control Professor Elke A. Rundensteiner.
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.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. –Because disk accesses are.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 16 – Intro. to Transactions.
Cs44321 CS4432: Database Systems II Lecture #19 Database Consistency and Transactions Professor Elke A. Rundensteiner.
Concurrent Transactions Even when there is no “failure,” several transactions can interact to turn a consistent state into an inconsistent state.
ICS 421 Spring 2010 Transactions & Concurrency Control (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
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.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
1 Concurrency Control and Recovery Module 6, Lecture 1.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
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.
Transaction Processing: Concurrency and Serializability 10/4/05.
Transactions or Concurrency Control. Introduction A program which operates on a DB performs 2 kinds of operations: –Access to the Database (Read/Write)
Cs4432recovery1 CS4432: Database Systems II Lecture #19 Database Consistency and Violations? Professor Elke A. Rundensteiner.
Concurrency. Correctness Principle A transaction is atomic -- all or none property. If it executes partly, an invalid state is likely to result. A transaction,
Database Management Systems I Alex Coman, Winter 2006
1 Introduction to Transaction Processing (1)
Concurrency Control 18.1 – 18.2 Chiu Luk CS257 Database Systems Principles Spring 2009.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #21 Concurrency Control Professor Elke A. Rundensteiner.
Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
TRANSACTIONS. Objectives Transaction Concept Transaction State Concurrent Executions Serializability Recoverability Implementation of Isolation Transaction.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Lecture 21 Ramakrishnan - Chapter 18.
CS 162 Discussion Section Week 9 11/11 – 11/15. Today’s Section ●Project discussion (5 min) ●Quiz (10 min) ●Lecture Review (20 min) ●Worksheet and Discussion.
Transaction Lectured by, Jesmin Akhter, Assistant professor, IIT, JU.
Introduction to Data Management CSE 344 Lecture 23: Transactions CSE Winter
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.
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
1 Transaction Processing Chapter Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu.
©Silberschatz, Korth and Sudarshan15.1Database System Concepts Chapter 15: Transactions Transaction Concept Transaction State Implementation of Atomicity.
Transactions. What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions developed.
Chapter 15: Transactions Loc Hoang CS 157B. Definition n A transaction is a discrete unit of work that must be completely processed or not processed at.
Chapter 14 Transactions Yonsei University 1 st Semester, 2015 Sanghyun Park.
1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Transactions.
Transaction Management and Recovery, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 16 – Intro. to Transactions.
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.
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
Concurrency Control.
Chapter 14: Transactions
CS216: Data-Intensive Computing Systems
Database Management System
Transaction Management
March 21st – Transactions
Transaction Management
Lecture 21: Concurrency & Locking
Transaction Management Overview
Transaction management
Transaction Management
CPSC-608 Database Systems
Transaction Management Overview
Temple University – CIS Dept. CIS616– Principles of Data Management
CPSC-608 Database Systems
Lecture 18: Concurrency Control
Presentation transcript:

CS4432: Database Systems II Transaction Management Motivation 1

2 DBMS Backend Components Our next focus

3 Transactions A transaction = sequence of operations that either all succeed, or all fail Basic unit of processing in DBMS Transactions have the ACID properties: A = atomicity C = consistency I = independence (Isolation) D = durability

Goal: The ACID properties A A tomicity: All actions in the transaction happen, or none happen. C C onsistency: If each transaction is consistent, and the DB starts consistent, it ends up consistent. I I solation: Execution of one transaction is isolated from that of all others. D D urability: If a transaction commits, its effects persist. 4

5 Data in the DB should be always correct and consistent Name White Green Gray Age Integrity & Consistency of Data How DBMS decides if data is consistent? Is this data correct (consistent)?

Schema-level Add Constraint command Business-constraint Use of Triggers 6 Define predicates and constraints that the data must satisfy Examples: - x is key of relation R - x  y holds in R -Domain(x) = {Red, Blue, Green} -No employee should make more than twice the average salary Integrity & Consistency Constraints Defining constraints (CS3431)

Example: a 1 + a 2 +…. a n = TOT ( constraint ) Deposit $100 in a 2 : a 2  a TOT  TOT FACT: DBMS is Not Consistent All the Time a2 TOT Initial stateFinal state Intermediate state Not A transaction hides intermediate states (Even under failure) A transaction hides intermediate states (Even under failure)

8 Transaction: a collection of actions that preserve consistency Consistent DBConsistent DB’ T If T starts with consistent state AND T executes in isolation THEN  T leaves consistent state Main Assumption Concept of Transactions

9 How Can Constraints Be Violated? Transaction Bug – The semantics of the transaction is wrong – E.g., update a2 and not ToT DBMS Bug – DBMS fails to detect inconsistent states Hardware Failure – Disk crash, memory failure, … Concurrent Access – Many transactions accessing the data at the same time – E.g., T1: give 10% raise to programmers T2: change programmers  systems analysts DBMS can easily detect and prevent that (if constraints are defined) Should not use this DBMS Our focus & Major components in DBMS

10 How Can We Prevent/Fix Violations? Chapter 17: Due to failures only Chapter 18: Due to concurrent access only Chapter 19: Due to failures and concurrent access

Plan of Attack (ACID properties) First we will deal with “ I ”, by focusing on concurrency control. Then we will address “ A ” and “ D ” by looking at recovery. What about “ C ” ? – Well, if you have the other three working, and you set up your integrity constraints correctly, then you get “C” for free 11

CS4432: Database Systems II Transaction Management Concurrency Control (Ch. 18) 12

13 T1 DB (consistency constraints) Concurrent Transactions T2T3 Tn Many transactions access the data at the same time Some are reading, others are writing May conflict

14 Transactions: Example T1:Read(A) T2:Read(A) A  A A  A  2 Write(A) Read(B) B  B+100 B  B  2 Write(B) Constraint: A=B How to execute these two transactions? How to schedule the read/write operations?

15 A Schedule An ordering of operations (reads/writes) inside one or more transactions over time What is correct outcome ? What is good schedule ? Leads To

16 Schedule A T1 T2 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 Serial Schedule: T1, T2

17 Schedule B Serial Schedule: T2, T1

18 Serial Schedules ! Definition: A schedule in which transactions are performed in a serial order (no interleaving) The Good: Consistency is guaranteed  Any serial schedule is “good”. The Bad: Throughput is low, need to execute in parallel Solution  Interleave Transactions in A Schedule…

19 Schedule C Schedule C is NOT serial but its Good

20 Schedule D Schedule C is NOT serial but its Bad Not Consistent

21 Schedule E Same as Schedule D but with new T2’ Same schedule as D, but this one is Good Consistent

22 What Is A ‘Good’ Schedule? Does not depend only on the sequence of operations – Schedules D and E have the same sequence – D produced inconsistent data – E produced consistent data We want schedules that are guaranteed “good” regardless of: – The initial state and – The transaction semantics Hence we consider only: – The order of read/write operations – Any other computations are ignored (transaction semantics) Transaction semantics played a role Example: Schedule S =r 1 (A) w 1 (A) r 2 (A) w 2 (A) r 1 (B) w 1 (B) r 2 (B) w 2 (B)

Example: Considering Only R/W Operations 23 Schedule S =r 1 (A) w 1 (A) r 2 (A) w 2 (A) r 2 (B) w 2 (B) r 1 (B) w 1 (B)

24 Concept: Conflicting Actions Conflicting actions: Two actions from two different transactions on the same object are conflicting iff one of them is write r1(A)  W2(A) w1(A)  r2(A) w1(A)  w2(A) r1(A)  r2(A)  Transaction 1 reads A, Transaction 2 write A  Transaction 1 writes A, Transaction 2 reads A  Transaction 1 writes A, Transaction 2 write A  Transaction 1 reads A, Transaction 2 reads A No Conflict Conflicting actions can cause anomalies…Which is Bad

Anomalies with Interleaving Reading Uncommitted Data (WR Conflicts, “dirty reads”): e.g. T1: A+100, B+100, T2: A*1.06, B*1.06 Unrepeatable Reads (RW Conflicts): E.g., T1: R(A), …..R(A), decrement, T2: R(A), decrement Overwriting Uncommitted Data (WW Conflicts): 25 We need schedule that is anomaly-free

Our Goal We need schedule that is equivalent to any serial schedule 26 It should allow interleaving Any serial order is good Produces consistent result & anomaly-free Given schedule S: If we can shuffle the non-conflicting actions to reach a serial schedule L  S is equivalent to L  S is good Given schedule S: If we can shuffle the non-conflicting actions to reach a serial schedule L  S is equivalent to L  S is good

27 Example: Schedule C

28 Example: Schedule C S c = r 1 (A) w 1 (A) r 2 (A) w 2 (A) r 1 (B) w 1 (B) r 2 (B) w 2 (B) S c ”= r 1 (A) w 1 (A) r 1 (B) w 1 (B) r 2 (A) w 2 (A) r 2 (B) w 2 (B) Can be switched because they are not conflicting T1T2  Schedule C is equivalent to a serial schedule  So it is “Good”

29 Why Schedule C turned out to be Good ? (Some Formalization) S c = r 1 (A) w 1 (A) r 2 (A) w 2 (A) r 1 (B) w 1 (B) r 2 (B) w 2 (B) T1  T2 (T1 precedes T2)  No cycles  S c is “equivalent” to a serial schedule where T 1 precedes T 2.

30 Example: Schedule D S D = r 1 (A) w 1 (A) r 2 (A) w 2 (A) r 2 (B) w 2 (B) r 1 (B) w 1 (B) Can we shuffle non-conflicting actions to make T1 T2 or T2 T1 ??

31 Example: Schedule D S D = r 1 (A) w 1 (A) r 2 (A) w 2 (A) r 2 (B) w 2 (B) r 1 (B) w 1 (B) Can we make T1 first  [T1 T2]? – No…Cannot move r 1 (B) w 1 (B) forward – Why: because r1(B) conflict with w2(B) so it cannot move….Same for w1(B)

32 Example: Schedule D S D = r 1 (A) w 1 (A) r 2 (A) w 2 (A) r 2 (B) w 2 (B) r 1 (B) w 1 (B) Can we make T2 first  [T2 T1]? – No…Cannot move r 2 (A) w 2 (A) forward – Why: because r2(A) conflict with w1(A) so it cannot move….Same for w2(A)  Schedule D is NOT equivalent to a serial schedule  So it is “Bad”

33 Why Schedule D turned out to be Bad? (Some Formalization) T1  T2 T2  T1 (T1 precedes T2) (T2 precedes T1)  Cycle Exist  S D is “Not equivalent” to any serial schedule. S D = r 1 (A) w 1 (A) r 2 (A) w 2 (A) r 2 (B) w 2 (B) r 1 (B) w 1 (B) T 1 T 2

Recap Serial Schedules are always “Good” (Consistency + no anomaly) – But they limit the throughput Goal: Find interleaving schedule that is “equivalent to” a serial schedule Identify “Conflicting Actions”, and try to arrange the non-conflicting ones to reach a serial schedule When formalized  Maps to Dependency Graphs and Cycle Testing 34 Next…

CS4432: Database Systems II Transaction Management Concurrency Control: Theory 35

Definitions Conflict Equivalent – S 1, S 2 are conflict equivalent schedules if S 1 can be transformed into S 2 by a series of swaps of non-conflicting actions. Conflict Serializable (Serializable for short ) – A schedule S 1 is conflict serializable if it is conflict equivalent to some serial schedule. 36  Schedule C is conflict serializable  Schedule D is not conflict serializable  Schedule C is conflict serializable  Schedule D is not conflict serializable

37 Answer: A Precedence Graph ! How to Determine This ? If no cyclesIf cycles Schedule is conflict serializable (Good) Schedule is NOT conflict serializable (Bad)

38 Nodes  Transactions in S Edges  Ti  Tj whenever the 3 conditions are met - 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 ) Two actions, one from Ti and one from Tj Ti’s action before Tj’s action They are conflicting actions

39 Precedence Graph Precedence graph for schedule S: – Nodes: Transactions in S – Edges: Ti → Tj whenever S: … r i (X) … w j (X) … S: … w i (X) … r j (X) … S: … w i (X) … w j (X) … Note: not necessarily consecutive

40 Graph Theory 101 Directed Graph: Cycle Not Cycle Nodes Directed edges

41 Theorem P(S 1 ) acyclic  S 1 conflict serializable

42 r2(x) r1(y) r1(z) r5(v) r5(w) w5(w)…. Time dim

Build P(A) 43  No cycles  Schedule A is Conflict Serializable

44 Exercise 1: 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 conflict-serializable?

45 Exercise 2: What is P(S) for S = w 1 (A) r 2 (A) r 3 (A) w 4 (A) ? Is S conflict-serializable?

Build P(F)….Is F Conflict Serializable ? 46 Exercise 3:

How to Find the Equivalent Serial Order 47  No cycles  Schedule A is Conflict Serializable So What is the serial order equivalent to A???

How to Find the Equivalent Serial Order 48 The serializability order can be obtained by a topological sorting of the graph. This is a linear order consistent with the partial order of the graph.  Take the transaction (T) with no incoming edges and put it in the serial order (left–to-right)  Delete T and its edges from the graph  Repeat until all transactions are taken  There can be many orders … It is not unqiue

How to Find the Equivalent Serial Order 49 One order  T5 T1 T2 T3 T4 Another order  T1 T3 T5 T2 T4 ….

CS4432: Database Systems II Concurrency Control Enforcing Serializability: Locking 50

Enforcing Serializable Schedules DBMSs use a “Scheduler” that schedules the actions of transactions Transactions send their requests (R or W) to Scheduler The scheduler prevents the formation of cycles – It grants permission to R or W only if no cycle will be formed 51

Locking Protocol “Scheduler” uses a locking protocol to enforce serializability Two New actions – Lock (exclusive): l i (A)  Transaction Ti locks item A – Unlock: Ui(A)  Transaction Ti unlocks (releases) item A 52 lock table

53 Rule #1: Well-Formed Transactions T i : … l i (A) … p i (A) … u i (A)... Any action (R/W) must be after the lock (l) and before the unlock (u) Rule 1 is at the level of each transaction independent of the others

54 Rule #2 Legal Scheduler S = …….. l i (A) ………... u i (A) ……... no l j (A) No transaction Tj can lock item A that is already locked by another transaction Ti (Transaction Tj must wait until Ti releases its lock) Rule 2 is at the level of the complete schedule (Set of interleaving transactions)

55 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:

56 Schedule F: Let’s Add Some Locking! Does the locking mechanism working? Does it guarantee serializable schedule??

Still Something is Missing… 57 Still by applying the locks….results is not consistent !!! Next: Rule #3 (Two-Phase Locking)