26
Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 03/16/22 Databases2 1

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

Embed Size (px)

Citation preview

Transactions

- Concurrent access & System failures- Properties of Transactions- Isolation Levels

04/18/23 Databases2 1

Motivated by two independent requirements

Concurrent database access Resilience to system failures

04/18/23 Databases2 2

Motivating Example Consider two transactions:

T1: BEGIN A=A+100, B=B-100 ENDT2: 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.

04/18/23 Databases2 3

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

04/18/23 Databases2 4

Concurrency GoalExecute 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 soBecause disk accesses are frequent, and relatively slow, it is important to keep the CPU humming by working on several user programs concurrently

04/18/23 Databases2 5

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

04/18/23 Databases2 6

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

04/18/23 Databases2 7

Transactions PropertiesACID Properties

A - Atomicity C - Consistency I – Isolation our main focus today D - Durability

04/18/23 Databases2 8

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

04/18/23 Databases2 9

The Standard Default Isolation Level

DBMS

Data

. . . SerializableOperations may be

interleaved, but executionmust be equivalent to some

sequential (serial) orderof all transactions

Overhead Reduction in concurrency

04/18/23 Databases2 10

Isolation Levels

DBMS

Data

. . .

Weaker “Isolation Levels”Read Uncommitted

Read Committed

Repeatable ReadSerializable

Overhead Concurrency

Consistency Guarantees

04/18/23 Databases2 11

Isolation LevelsPer transaction “In the eye of the beholder”

DBMS

Data

. . .My transaction isRepeatable ReadRead

My transaction isReadRead Uncommitted

04/18/23 Databases2 12

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

04/18/23 Databases2 13

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

04/18/23 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

04/18/23 Databases2 15

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

04/18/23 Databases2 16

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

04/18/23 Databases2 17

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;

04/18/23 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

04/18/23 Databases2 19

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

04/18/23 Databases2 20

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

04/18/23 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

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;

04/18/23 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

concurrent with …

Insert Into Student [ 100 new tuples ]

Set Transaction Isolation Level Repeatable Read;

Select Avg(GPA) From Student;Select Max(GPA) From Student;

04/18/23 Databases2 23

Isolation Levels: Summary

dirty readsnonrepeatable

reads phantoms

Read Uncommitted

Read Committed

Repeatable Read

Serializable

04/18/23 Databases2 24

Isolation Levels: Summary

dirty readsnonrepeatable

reads phantoms

Read Uncommitted Y Y Y

Read Committed N Y Y

Repeatable Read N N Y

Serializable N N N

04/18/23 Databases2 25

Transactions: solution for both concurrency & failuresProperties: A,C,I,DIsolation 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

04/18/23 Databases2 26