Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/9/2015 Databases2 1 Motivated by two independent requirements Concurrent database access Resilience to system failures 4/9/2015 Databases2 2 Motivating Example Consider T1: T2: two transactions: BEGIN A=A+100, B=B-100 END BEGIN A=1.06*A, B=1.06*B END Intuitively, the first transaction is transferring $100 from B’s account to A’s account. The second is crediting both accounts with a 6% interest payment. 4/9/2015 Databases2 3 Example (Contd.) Consider T1: T2: A=A+100, A=1.06*A, B=B-100 B=1.06*B This is OK. But what about: T1: T2: a possible interleaving: A=A+100, A=1.06*A, B=1.06*B B=B-100 What if system crashes in the middle of a Transaction?: 4/9/2015 Databases2 4 Concurrency Goal Execute sequence of SQL statements so they appear to be running in isolation Simple solution: execute them in isolation? But want to enable concurrency whenever safe to do so Because disk accesses are frequent, and relatively slow, it is important to keep the CPU humming by working on several user programs concurrently 4/9/2015 Databases2 5 Solution for both concurrency and failures Transactions A transaction is a sequence of one or more SQL operations treated as a unit Transactions appear to run in isolation If the system fails, each transaction’s changes are reflected either entirely or not at all 4/9/2015 Databases2 6 Solution for both concurrency and failures Transactions A transaction is a sequence of one or more SQL operations treated as a unit. In terms of SQL standard: Transaction begins automatically on first SQL statement On “commit” transaction ends and new one begins Current transaction ends on session termination “Autocommit” turns each statement into transaction 4/9/2015 Databases2 7 Transactions Properties ACID Properties A - Atomicity C - Consistency I – Isolation our main focus today D - Durability 4/9/2015 Databases2 8 Transactions Properties ACID Properties A – Atomicity: If T does not commit (e.g. system crashes during execution of T), the transaction rolls back. “All or nothing”. C – Consistency: T can assume that all integrity constraints hold when T begins; T must guarantee they hold when it ends I – Isolation D – Durability: If system crashes after transaction commits, all affects remain in the database. 4/9/2015 Databases2 9 The Standard Default Isolation Level . . . Serializable Operations may be interleaved, but execution must be equivalent to some sequential (serial) order of all transactions DBMS Overhead Reduction in concurrency Data 4/9/2015 Databases2 10 Isolation Levels Weaker “Isolation Levels” . . . DBMS Read Uncommitted Read Committed Repeatable Read Serializable Overhead Concurrency Consistency Guarantees Data 4/9/2015 Databases2 11 Isolation Levels Per transaction “In the eye of the beholder” My transaction is Repeatable Read . . . My transaction is Read Uncommitted DBMS Data 4/9/2015 Databases2 12 Dirty Reads “Dirty” data item: written by an uncommitted transaction Update Student Set GPA = (1.1) GPA Where sizeHS > 2500 concurrent with … Select Avg(GPA) From Student 4/9/2015 Databases2 13 The Isolation Level Read Uncommitted A transaction may perform dirty reads Update Student Set GPA = (1.1) GPA Where sizeHS > 2500 concurrent with … Set Transaction Isolation Level Read Uncommitted; Select Avg(GPA) From Student; 4/9/2015 Databases2 14 Q Consider a table R(A) containing {(1),(2)} and two transactions: T1: update R set A=2*A; T2: select avg(A) from R; If T2 executes using “read uncommitted”, what are the possible values it returns? [ ] 1.5, 2, 2.5, 3 [ ] 1.5, 2, 3 [ ] 1.5, 2.5, 3 [ ] 1.5, 3 4/9/2015 Databases2 15 A Consider a table R(A) containing {(1),(2)} and two transactions: T1: update R set A=2*A; T2: select avg(A) from R; If T2 executes using “read uncommitted”, what are the possible values it returns? [+] 1.5, 2, 2.5, 3 [ ] 1.5, 2, 3 Explanation: The update [ ] 1.5, 2.5, 3 command in T1 can update the values in either order, and the [ ] 1.5, 3 select in T2 can compute the avg at any point before, between, or after the updates. 4/9/2015 Databases2 16 The Isolation Level Read Committed A transaction may not perform dirty reads Update Student Set GPA = (1.1) GPA Where sizeHS > 2500 concurrent with … Set Transaction Isolation Level Read Committed; Select Avg(GPA) From Student; 4/9/2015 Databases2 17 The Isolation Level Read Committed A transaction may not perform dirty reads Still does not guarantee global serializability Update Student Set GPA = (1.1) GPA Where sizeHS > 2500 concurrent with … Set Transaction Isolation Level Read Committed; Select Avg(GPA) From Student; Select Max(GPA) From Student; 4/9/2015 Databases2 18 Q Consider tables R(A) and S(B),both containing {(1),(2)}, and two transactions: T1: update R set A=2*A; update S set B=2*B; T2: select avg(A) from R; select avg(B) from S; If T2 executes using “read committed”, is it possible for T2 to return two different values? [ ] No [ ] Yes 4/9/2015 Databases2 19 A Consider tables R(A) and S(B),both containing {(1),(2)}, and two transactions: T1: update R set A=2*A; update S set B=2*B; T2: select avg(A) from R; select avg(B) from S; If T2 executes using “read committed”, is it possible for T2 to return two different values? [ ] No Explanation: T2 could return avg(A) computed before T1, [+] Yes and avg(B) computed after T1. 4/9/2015 Databases2 20 The Isolation Level Repeatable Read A transaction may not perform dirty reads An item read multiple times cannot change value Update Student Set GPA = (1.1) GPA Where sizeHS > 2500 concurrent with … Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Max(GPA) From Student; 4/9/2015 Databases2 21 The Isolation Level Repeatable Read A transaction may not perform dirty reads An item read multiple times cannot change value Still does not guarantee global serializability Update Student Set GPA = (1.1) GPA; Update Student Set sizeHS = 1500 Where sID = 123; concurrent with … Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Avg(sizeHS) From Student; 4/9/2015 Databases2 22 Isolation Level Repeatable Read A transaction may not perform dirty reads An item read multiple times cannot change value But a relation can change: “phantom” tuples Insert Into Student [ 100 new tuples ] concurrent with … Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Max(GPA) From Student; 4/9/2015 Databases2 23 Isolation Levels: Summary dirty reads nonrepeatable reads phantoms Read Uncommitted Read Committed Repeatable Read Serializable 4/9/2015 Databases2 24 Isolation Levels: Summary dirty reads nonrepeatable reads phantoms Read Uncommitted Y Y Y Read Committed N Y Y Repeatable Read N N Y Serializable N N N 4/9/2015 Databases2 25 Summing up Transactions: solution for both concurrency & failures Properties: A,C,I,D Isolation Levels Standard default: Serializable Weaker isolation levels – Increased concurrency + decreased overhead = increased performance – Weaker consistency guarantees – Some systems have default Repeatable Read Isolation level per transaction and “eye of the beholder” – Each transaction’s reads must conform to its isolation level 4/9/2015 Databases2 26