1 Lecture 10: Transactions. 2 The Setting uDatabase systems are normally being accessed by many users or processes at the same time. wBoth queries and.

Slides:



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

Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/13/2015Databases21.
Unit 9 Concurrency Control. 9-2 Wei-Pang Yang, Information Management, NDHU Content  9.1 Introduction  9.2 Locking Technique  9.3 Optimistic Concurrency.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Introduction to Database Systems1 Concurrency Control CC.Lecture 1.
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Chapter 15: Transactions Transaction Concept Transaction Concept Concurrent Executions Concurrent Executions Serializability Serializability Testing for.
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.
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.
Transactions (Chapter ). What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions.
Notes on Chapter 8 Transactions from Chapter 6 Views and Indexes from Chapter 8.
Transactions and Locking Rose-Hulman Institute of Technology Curt Clifton.
Quick Review of Apr 29 material
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
1 Chapter 7 System Aspects of SQL uSQL in a Programming Environment uTransactions uAuthorization.
1 Transactions Serializability Isolation Levels Atomicity.
Winter 2002Arthur Keller – CS 18013–1 Schedule Today: Feb. 21 (TH) u Transactions, Authorization. u Read Sections Project Part 5 due. Feb. 26.
1 Transactions, Views, Indexes Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data.
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.
Fall 2001Arthur Keller – CS 18012–1 Schedule Nov. 6 (T) Transactions, Authorization. u Read Sections Nov. 8 (TH) Object-Oriented Database Design.
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
Transactions Amol Deshpande CMSC424. Today Project stuff… Summer Internships 
Transactions Controlling Concurrent Behavior. Why Transactions? Database systems are normally being accessed by many users or processes at the same time.
Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control.
Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,
Transactions Sylvia Huang CS 157B. Transaction A transaction is a unit of program execution that accesses and possibly updates various data items. A transaction.
DB Transactions CS143 Notes TRANSACTION: A sequence of SQL statements that are executed "together" as one unit:
TRANSACTIONS. Objectives Transaction Concept Transaction State Concurrent Executions Serializability Recoverability Implementation of Isolation Transaction.
1 What we have covered? uIndexing and Hashing uData warehouse and OLAP uData Mining uInformation Retrieval and Web Mining uXML and XQuery uSpatial Databases.
SCUJoAnne Holliday11–1 Schedule Today: u Transaction concepts. u Read Sections Next u Authorization and security.
Winter 2006Keller, Ullman, Cushing13–1 TRANSACTION MANAGEMENT Airline Reservationsmany updates Statistical Abstract of the USmany queries Atomicity – all.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
Extended Operators in SQL and Relational Algebra Zaki Malik September 11, 2008.
Chapter 14 Transactions Yonsei University 1 st Semester, 2015 Sanghyun Park.
1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner.
©Silberschatz, Korth and Sudarshan14.1Database System Concepts - 6 th Edition Chapter 14: Transactions Transaction Concept Transaction State Concurrent.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Transactions.
Concurrency (cont.) Schedule. In multiprogramming environment, Several transaction run concurrently Database consistency can be destroy Schedules to ensure.
Transactions, Views, Indexes Introduction to Transactions: Controlling Concurrent Behavior Virtual and Materialized Views Indexes: Speeding Accesses to.
1 SQL Authorization (Chap. 8.7) Privileges Grant and Revoke Grant Diagrams.
1 Transactions Serializability Isolation Levels Atomicity.
1 Transactions, Views, Indexes Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data This slides are from J. Ullman’s.
1 Transaction Processing Case Study. 2 Interaksi Proses There is table Sells(shop,beverage,price), and suppose that Joe’s Shop sells only Juice for $2.50.
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:
TRANSACTION PROCESSING 1. 2 Why Transactions? uDatabase systems are normally being accessed by many users or processes at the same time. wBoth queries.
Concurrency Control.
Chapter 14: Transactions
CS216: Data-Intensive Computing Systems
Database Management System
Schedule Today Transactions, Authorization. Sections
Transaction Management
Transaction Processing
CPSC-310 Database Systems
Transactions.
March 21st – Transactions
Temple University – CIS Dept. CIS661 – Principles of Data Management
Transactions Properties.
Transactions.
Chapter 15: Transactions
Transaction Management
Transactions Sylvia Huang CS 157B.
Transaction Management Overview
Transaction management
C. Faloutsos Transactions
Temple University – CIS Dept. CIS616– Principles of Data Management
Transactions, Views, Indexes
-Transactions in SQL -Constraints and Triggers
Presentation transcript:

1 Lecture 10: Transactions

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

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

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

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

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

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

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

9 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’;

10 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);

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

12 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.00 (max) 3.00

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

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

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

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

17 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

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

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

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

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

22 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 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).

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

24 Concurrency Control T1T2…Tn DB (consistency constraints)

25 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

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

27 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

28 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

29 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

30 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

31 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’

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

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

34 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

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

36 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

37 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

38 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

39 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

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

41 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

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

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

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

45 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?

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

47 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

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

49 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

50 (  ) 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