Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/13/2015Databases21.

Similar presentations


Presentation on theme: "Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/13/2015Databases21."— Presentation transcript:

1 Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/13/2015Databases21

2 Motivated by two independent requirements  Concurrent database access  Resilience to system failures 4/13/2015Databases22

3 Motivating Example  Consider two transactions: T1:BEGIN A=A+100, B=B-100 END T2: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/13/2015Databases23

4 Example (Contd.)  Consider a possible interleaving: T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B  This is OK. But what about: T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B  What if system crashes in the middle of a Transaction?: 4/13/2015Databases24

5 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/13/2015Databases25

6 Solution for both concurrency and failures 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 Transactions 4/13/2015Databases26

7 Solution for both concurrency and failures 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 Transactions 4/13/2015Databases27

8 Transactions Properties ACID Properties A - Atomicity C - Consistency I – Isolation  our main focus today D - Durability 4/13/2015Databases28

9 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/13/2015Databases29

10 The Standard Default Isolation Level DBMS Data... Serializable Operations may be interleaved, but execution must be equivalent to some sequential (serial) order of all transactions  Overhead  Reduction in concurrency 4/13/2015Databases210

11 Isolation Levels DBMS Data... Weaker “Isolation Levels” Read Uncommitted Read Committed Repeatable Read Serializable  Overhead  Concurrency  Consistency Guarantees 4/13/2015Databases211

12 Isolation Levels  Per transaction  “In the eye of the beholder” DBMS Data... My transaction is Read Repeatable Read My transaction is Read Read Uncommitted 4/13/2015Databases212

13 Dirty Reads “Dirty” data item: written by an uncommitted transaction concurrent with … Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500 Select Avg(GPA) From Student 4/13/2015Databases213

14 The Isolation Level Read Uncommitted  A transaction may perform dirty reads Set Transaction Isolation Level Read Uncommitted; Select Avg(GPA) From Student; concurrent with … Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500 4/13/2015Databases214

15 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/13/2015Databases215

16 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 [ ] 1.5, 2.5, 3 [ ] 1.5, 3 Explanation: The update command in T1 can update the values in either order, and the select in T2 can compute the avg at any point before, between, or after the updates. 4/13/2015Databases216

17 The Isolation Level Read Committed  A transaction may not perform dirty reads Set Transaction Isolation Level Read Committed; Select Avg(GPA) From Student; concurrent with … Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500 4/13/2015Databases217

18 The Isolation Level Read Committed  A transaction may not perform dirty reads Still does not guarantee global serializability concurrent with … Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500 Set Transaction Isolation Level Read Committed; Select Avg(GPA) From Student; Select Max(GPA) From Student; 4/13/2015Databases218

19 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/13/2015Databases219

20 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 [+] Yes Explanation: T2 could return avg(A) computed before T1, and avg(B) computed after T1. 4/13/2015Databases220

21 The Isolation Level Repeatable Read  A transaction may not perform dirty reads  An item read multiple times cannot change value concurrent with … Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Max(GPA) From Student; Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500 4/13/2015Databases221

22 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 concurrent with … Update Student Set GPA = (1.1)  GPA; Update Student Set sizeHS = 1500 Where sID = 123; Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Avg(sizeHS) From Student; 4/13/2015Databases222

23 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 concurrent with … Insert Into Student [ 100 new tuples ] Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Max(GPA) From Student; 4/13/2015Databases223

24 Isolation Levels: Summary dirty reads nonrepeatable reads phantoms Read Uncommitted Read Committed Repeatable Read Serializable 4/13/2015Databases224

25 Isolation Levels: Summary dirty reads nonrepeatable reads phantoms Read Uncommitted YYY Read Committed NYY Repeatable Read NNY Serializable NNN 4/13/2015Databases225

26 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 Summing up 4/13/2015Databases226


Download ppt "Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/13/2015Databases21."

Similar presentations


Ads by Google