1 of 21

CSE 344: Section 5

Transactions and �Isolation Levels

April 30, 2020

2 of 21

Conflicts

READ-WRITE

WRITE-READ

WRITE-WRITE

Intra-transactional: don’t mess with the order of operations within a transaction

}

Inter-transactional: represent with a conflict graph

3 of 21

Serializability

“Conflict serializable” is a stronger constraint than “serializable”

I.e. Any schedule that is conflict serializable must be serializable.

Serializable

Conflict

Serializable

Easy to check

Not easy to check

4 of 21

Serializability

Checking for conflict serializability -> precedence graph and cycle checking

5 of 21

Serializability

S1: w1(Y); w2(Y); w1(X); w2(X); w3(X)

S2: w1(Y); w2(Y); w2(X); w1(X); w3(X)

Are these serializable? Conflict serializable?

6 of 21

Serializability

S1: w1(Y); w2(Y); w1(X); w2(X); w3(X)

S2: w1(Y); w2(Y); w2(X); w1(X); w3(X)

Conflict Serializable

Serializable (but not conflict serializable)

7 of 21

2PL v.s. Strict 2PL

2PL:

  • In every transaction, all lock requests must precede all unlock requests
  • Ensure conflict serializability
  • Might not be able to recover (Dirty Read: Read on some write that gets rolled back)

Strict 2PL:

  • Every lock each transaction is held until commit or abort
  • Ensure conflict serializability
  • Recoverable as each transaction does not affect others until commit/abort

8 of 21

2PL v.s. Strict 2PL

Invalid reads

Trickled unlock and transaction end

9 of 21

2PL Variants

2PL

Ensures conflict serializability

Strict 2PL

Ensures recovery

Conservative 2PL

Ensures deadlock-free

10 of 21

Isolation Level: Read Uncommitted

Write Locks? Strict 2PL

Read Locks? None (Immediate Read)

Problem: Dirty-Read

Reading uncommitted data that can be rolled back

SET TRANSACTION ISOLATION LEVEL... READ UNCOMMITTED

READ COMMITTED

REPEATABLE READ

SERIALIZABLE

11 of 21

Isolation Level: Read Uncommitted

T2 is reading value of A updated by T1’s write on A, but T1 has not committed yet.

The value of A read by T2 might not even be in the result.

Then T2’s action can be influenced by such uncommitted data.

T1

T2

W(A)

R(A)

W(B)

Commit

R(B)

Commit

SET TRANSACTION ISOLATION LEVEL... READ UNCOMMITTED

READ COMMITTED

REPEATABLE READ

SERIALIZABLE

12 of 21

Isolation Level: Read Committed

Write Locks? Strict 2PL

Read Locks? Obtain before read, release after (No more dirty read)

Problem: Unrepeatable Read

The values of 2 reads on the same tuple can be different in the same transaction

SET TRANSACTION ISOLATION LEVEL... READ UNCOMMITTED

READ COMMITTED

REPEATABLE READ

SERIALIZABLE

13 of 21

Isolation Level: Read Committed

T1’s first R(A) and T1’s second R(A) might have different results.

Updated by T2’s W(A).

SET TRANSACTION ISOLATION LEVEL... READ UNCOMMITTED

READ COMMITTED

REPEATABLE READ

SERIALIZABLE

T1

T2

R(A)

R(A)

W(A)

Commit

R(A)

W(A)

Commit

14 of 21

Isolation Level: Repeatable Read

Write Locks? Strict 2PL

Read Locks? Strict 2PL (No more unrepeatable read)

“The transaction waits until rows write-locked by other transactions are unlocked; this prevents it from reading any "dirty" data.”

Problem: Phantom Read

In the same transaction, some tuples appear sometimes and disappear other times

SET TRANSACTION ISOLATION LEVEL... READ UNCOMMITTED

READ COMMITTED

REPEATABLE READ

SERIALIZABLE

15 of 21

STRICT 2PL READS MEAN MY TXNS ARE SERIALIZABLE

I CAN INSERT A NEW ITEM WHENEVER

16 of 21

Isolation Level: Repeatable Read

SET TRANSACTION ISOLATION LEVEL... READ UNCOMMITTED

READ COMMITTED

REPEATABLE READ

SERIALIZABLE

Finds A1, A2

Finds A1, A2, A3

OK since A3 did not exist before

17 of 21

Isolation Level: Serializable

Not the same thing as a serial schedule!!!

Write Locks: Strict 2PL

Read Locks: Strict 2PL

Additional Predicate Lock/Table Lock (No Phantom)

SET TRANSACTION ISOLATION LEVEL... READ UNCOMMITTED

READ COMMITTED

REPEATABLE READ

SERIALIZABLE

18 of 21

Isolation Level: Serializable

Predicate Lock Example:

In Transaction T, we have a statement:

SELECT * FROM People WHERE age > 18;

In this case, the transaction will grab a predicate lock that prevents inserting and deleting tuples that can affect the predicate/statement.

In this case, the lock prevents inserting and deleting tuples with age > 18.

SET TRANSACTION ISOLATION LEVEL... READ UNCOMMITTED

READ COMMITTED

REPEATABLE READ

SERIALIZABLE

19 of 21

Isolation Level: Summary

Usually not the default!

20 of 21

Isolation Levels Reinforced

Isolation Level

Read Lock Behavior

Conflict Example

Conflict Types

Read Uncommitted

No Read Locks

WR or RW conflict

“Dirty reads”

Read Committed

Short-Duration Read Locks

RWR conflict

“Unrepeatable reads”

Repeatable Read

Long-Duration Read Locks (Strict 2PL)

Phantom conflict

Phantom problem

Serializable

Predicate Locks

Serializable

None

21 of 21

Isolation Level: Summary

Make sure you can associate the following:

Isolation level name <-> Main problem <-> Locking protocol

Practice:

Identifying isolation levels from a schedule

Coming up with a schedule from an isolation level