transactions - University of Washington
Transcript of transactions - University of Washington
![Page 1: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/1.jpg)
CSE 344MARCH 7TH – TRANSACTIONS
![Page 2: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/2.jpg)
ADMINISTRIVIA• HW7 Due Tonight 11:30• HW8 Due Monday
• Max Two Late days• Exam Review
• Sunday: 5pm EEB 045• Section tomorrow
• Fair game for final – slides posted
![Page 3: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/3.jpg)
TRANSACTIONSWe use database transactions everyday
• Bank $$$ transfers• Online shopping• Signing up for classes
For this class, a transaction is a series of DB queries• Read / Write / Update / Delete / Insert• Unit of work issued by a user that is independent from others
![Page 4: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/4.jpg)
CHALLENGESWant to execute many apps concurrently
• All these apps read and write data to the same DB
Simple solution: only serve one app at a time• What’s the problem?
Want: multiple operations to be executed atomically over the same DBMS
![Page 5: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/5.jpg)
ACIDAtomicConsistentIsolatedDurable
Again: by default each statement is its own txn• Unless auto-commit is off then each statement starts a
new txn
![Page 6: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/6.jpg)
SERIAL SCHEDULE
A serial schedule is one in which transactions are executed one after the other, in some sequential order
Fact: nothing can go wrong if the system executes transactions serially
• (up to what we have learned so far)• But DBMS don’t do that because we want better overall system
performance
![Page 7: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/7.jpg)
EXAMPLE
T1 T2READ(A, t) READ(A, s)t := t+100 s := s*2WRITE(A, t) WRITE(A,s)READ(B, t) READ(B,s)t := t+100 s := s*2WRITE(B,t) WRITE(B,s)
A and B are elementsin the database
t and s are variables in txn source code
![Page 8: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/8.jpg)
EXAMPLE OF A (SERIAL) SCHEDULE
T1 T2READ(A, t)t := t+100WRITE(A, t)READ(B, t)t := t+100WRITE(B,t)
READ(A,s)s := s*2WRITE(A,s)READ(B,s)s := s*2WRITE(B,s)
Tim
e
![Page 9: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/9.jpg)
ANOTHER SERIAL SCHEDULE
T1 T2READ(A,s)s := s*2WRITE(A,s)READ(B,s)s := s*2WRITE(B,s)
READ(A, t)t := t+100WRITE(A, t)READ(B, t)t := t+100WRITE(B,t)
Tim
e
![Page 10: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/10.jpg)
REVIEW: SERIALIZABLE SCHEDULE
A schedule is serializable if it is equivalent to a serial schedule
![Page 11: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/11.jpg)
A SERIALIZABLESCHEDULE
T1 T2READ(A, t)t := t+100WRITE(A, t)
READ(A,s)s := s*2WRITE(A,s)
READ(B, t)t := t+100WRITE(B,t)
READ(B,s)s := s*2WRITE(B,s)
This is a serializable schedule.This is NOT a serial schedule
![Page 12: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/12.jpg)
A NON-SERIALIZABLE SCHEDULE
T1 T2READ(A, t)t := t+100WRITE(A, t)
READ(A,s)s := s*2WRITE(A,s)READ(B,s)s := s*2WRITE(B,s)
READ(B, t)t := t+100WRITE(B,t)
![Page 13: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/13.jpg)
HOW DO WE KNOW IF A SCHEDULE IS SERIALIZABLE?
T1: r1(A); w1(A); r1(B); w1(B)T2: r2(A); w2(A); r2(B); w2(B)
Notation:
Key Idea: Focus on conflicting operations
![Page 14: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/14.jpg)
CONFLICTS
Write-Read – WRRead-Write – RWWrite-Write – WWRead-Read?
![Page 15: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/15.jpg)
CONFLICT SERIALIZABILITY
Conflicts: (i.e., swapping will change program behavior)
ri(X); wi(Y)Two actions by same transaction Ti:
wi(X); wj(X)Two writes by Ti, Tj to same element
wi(X); rj(X)Read/write by Ti, Tj to same element
ri(X); wj(X)
![Page 16: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/16.jpg)
CONFLICT SERIALIZABILITY
A schedule is conflict serializable if it can be transformed into a serial schedule by a series of swappings of adjacent non-conflicting actions
Every conflict-serializable schedule is serializableThe converse is not true (why?)
![Page 17: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/17.jpg)
CONFLICT SERIALIZABILITY
Example:r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B)
![Page 18: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/18.jpg)
CONFLICT SERIALIZABILITY
Example:
r1(A); w1(A); r1(B); w1(B); r2(A); w2(A); r2(B); w2(B)
r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B)
![Page 19: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/19.jpg)
CONFLICT SERIALIZABILITY
Example:
r1(A); w1(A); r1(B); w1(B); r2(A); w2(A); r2(B); w2(B)
r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B)
r1(A); w1(A); r2(A); r1(B); w2(A); w1(B); r2(B); w2(B)
r1(A); w1(A); r1(B); r2(A); w2(A); w1(B); r2(B); w2(B)
….
![Page 20: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/20.jpg)
TESTING FOR CONFLICT-SERIALIZABILITY
Precedence graph:• A node for each transaction Ti, • An edge from Ti to Tj whenever an action in Ti
conflicts with, and comes before an action in Tj
The schedule is conflict-serializable iff the precedence graph is acyclic
![Page 21: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/21.jpg)
EXAMPLE 1
r2(A); r1(B); w2(A); r3(A); w1(B); w3(A); r2(B); w2(B)
1 2 3
![Page 22: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/22.jpg)
EXAMPLE 1
r2(A); r1(B); w2(A); r3(A); w1(B); w3(A); r2(B); w2(B)
1 2 3
This schedule is conflict-serializable
AB
![Page 23: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/23.jpg)
EXAMPLE 2
r2(A); r1(B); w2(A); r2(B); r3(A); w1(B); w3(A); w2(B)
1 2 3
![Page 24: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/24.jpg)
EXAMPLE 2
1 2 3
This schedule is NOT conflict-serializable
A
B
B
r2(A); r1(B); w2(A); r2(B); r3(A); w1(B); w3(A); w2(B)
![Page 25: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/25.jpg)
SCHEDULER
Scheduler = the module that schedules the transaction’s actions, ensuring serializability
Also called Concurrency Control Manager
We discuss next how a scheduler may be implemented
![Page 26: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/26.jpg)
IMPLEMENTING A SCHEDULER
Major differences between database vendorsLocking Scheduler
• Aka “pessimistic concurrency control”• SQLite, SQL Server, DB2
Multiversion Concurrency Control (MVCC)• Aka “optimistic concurrency control”• Postgres, Oracle: Snapshot Isolation (SI)
We discuss only locking schedulers in this class
![Page 27: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/27.jpg)
LOCKING SCHEDULERSimple idea:Each element has a unique lockEach transaction must first acquire the lock before reading/writing that elementIf the lock is taken by another transaction, then waitThe transaction must release the lock(s)
By using locks scheduler ensures conflict-serializability
![Page 28: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/28.jpg)
WHAT DATA ELEMENTS ARE LOCKED?
Major differences between vendors:
Lock on the entire database• SQLite
Lock on individual records• SQL Server, DB2, etc
![Page 29: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/29.jpg)
CASE STUDY: SQLITE
SQLite is very simpleMore info: http://www.sqlite.org/atomiccommit.html
Lock types• READ LOCK (to read)• RESERVED LOCK (to write)• PENDING LOCK (wants to commit)• EXCLUSIVE LOCK (to commit)
![Page 30: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/30.jpg)
SQLITEStep 1: when a transaction begins
Acquire a READ LOCK (aka "SHARED" lock)All these transactions may read happilyThey all read data from the database fileIf the transaction commits without writing anything, then it simply releases the lock
![Page 31: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/31.jpg)
SQLITE
Step 2: when one transaction wants to writeAcquire a RESERVED LOCKMay coexists with many READ LOCKsWriter TXN may write; these updates are only in main memory; others don't see the updatesReader TXN continue to read from the fileNew readers acceptedNo other TXN is allowed a RESERVED LOCK
![Page 32: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/32.jpg)
SQLITE
Step 3: when writer transaction wants to commit,it needs exclusive lock, which can’t coexists with read locks
Acquire a PENDING LOCKMay coexists with old READ LOCKsNo new READ LOCKS are acceptedWait for all read locks to be released
Why not writeto disk right now?
![Page 33: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/33.jpg)
SQLITE
Step 4: when all read locks have been releasedAcquire the EXCLUSIVE LOCKNobody can touch the database nowAll updates are written permanently to the database file
Release the lock and COMMIT
![Page 34: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/34.jpg)
SQLITE
None READLOCK
RESERVEDLOCK
PENDINGLOCK
EXCLUSIVELOCK
commit executed
begin transaction first write no more read lockscommit requested
commit
![Page 35: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/35.jpg)
SCHEDULE ANOMALIESWhat could go wrong if we didn’t have concurrency control:
• Dirty reads (including inconsistent reads)• Unrepeatable reads• Lost updates
Many other things can go wrong too
![Page 36: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/36.jpg)
DIRTY READS
T1: WRITE(A)
T1: ABORT
T2: READ(A)
Write-Read Conflict
![Page 37: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/37.jpg)
INCONSISTENT READ
T1: A := 20; B := 20;T1: WRITE(A)
T1: WRITE(B)
T2: READ(A);T2: READ(B);
Write-Read Conflict
![Page 38: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/38.jpg)
UNREPEATABLE READ
T1: WRITE(A) T2: READ(A);
T2: READ(A);
Read-Write Conflict
![Page 39: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/39.jpg)
LOST UPDATE
T1: READ(A)
T1: A := A+5
T1: WRITE(A)
T2: READ(A);
T2: A := A*1.3
T2: WRITE(A);
Write-Write Conflict
![Page 40: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/40.jpg)
MORE NOTATIONS
Li(A) = transaction Ti acquires lock for element A
Ui(A) = transaction Ti releases lock for element A
![Page 41: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/41.jpg)
A NON-SERIALIZABLESCHEDULE
T1 T2READ(A)A := A+100WRITE(A)
READ(A)A := A*2WRITE(A)READ(B)B := B*2WRITE(B)
READ(B)B := B+100WRITE(B)
![Page 42: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/42.jpg)
EXAMPLET1 T2L1(A); READ(A)A := A+100WRITE(A); U1(A); L1(B)
L2(A); READ(A)A := A*2WRITE(A); U2(A); L2(B); BLOCKED…
READ(B)B := B+100WRITE(B); U1(B);
…GRANTED; READ(B)B := B*2WRITE(B); U2(B);
Scheduler has ensured a conflict-serializable schedule
![Page 43: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/43.jpg)
BUT…T1 T2L1(A); READ(A)A := A+100WRITE(A); U1(A);
L2(A); READ(A)A := A*2WRITE(A); U2(A);L2(B); READ(B)B := B*2WRITE(B); U2(B);
L1(B); READ(B)B := B+100WRITE(B); U1(B);
Locks did not enforce conflict-serializability !!! What’s wrong ?
![Page 44: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/44.jpg)
TWO PHASE LOCKING (2PL)
In every transaction, all lock requests must precede all unlock requests
The 2PL rule:
![Page 45: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/45.jpg)
EXAMPLE: 2PL TRANSACTIONST1 T2L1(A); L1(B); READ(A)A := A+100WRITE(A); U1(A)
L2(A); READ(A)A := A*2WRITE(A); L2(B); BLOCKED…
READ(B)B := B+100WRITE(B); U1(B);
…GRANTED; READ(B)B := B*2WRITE(B); U2(A); U2(B); Now it is conflict-serializable
![Page 46: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/46.jpg)
TWO PHASE LOCKING (2PL)
Theorem: 2PL ensures conflict serializability
![Page 47: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/47.jpg)
TWO PHASE LOCKING (2PL)
Theorem: 2PL ensures conflict serializability
Proof. Suppose not: thenthere exists a cyclein the precedence graph.
T1
T2
T3
BA
C
![Page 48: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/48.jpg)
TWO PHASE LOCKING (2PL)
Theorem: 2PL ensures conflict serializability
Proof. Suppose not: thenthere exists a cyclein the precedence graph.
T1
T2
T3
BA
C
Then there is thefollowing temporalcycle in the schedule:
![Page 49: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/49.jpg)
TWO PHASE LOCKING (2PL)
Theorem: 2PL ensures conflict serializability
Proof. Suppose not: thenthere exists a cyclein the precedence graph.
T1
T2
T3
BA
C
Then there is thefollowing temporalcycle in the schedule:U1(A)àL2(A) why?
U1(A) happenedstrictly before L2(A)
![Page 50: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/50.jpg)
TWO PHASE LOCKING (2PL)
Theorem: 2PL ensures conflict serializability
Proof. Suppose not: thenthere exists a cyclein the precedence graph.
T1
T2
T3
BA
C
Then there is thefollowing temporalcycle in the schedule:U1(A)àL2(A) why?
![Page 51: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/51.jpg)
TWO PHASE LOCKING (2PL)
Theorem: 2PL ensures conflict serializability
Proof. Suppose not: thenthere exists a cyclein the precedence graph.
T1
T2
T3
BA
C
Then there is thefollowing temporalcycle in the schedule:U1(A)àL2(A) L2(A)àU2(B) why?
L2(A) happenedstrictly before U1(A)
![Page 52: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/52.jpg)
TWO PHASE LOCKING (2PL)
Theorem: 2PL ensures conflict serializability
Proof. Suppose not: thenthere exists a cyclein the precedence graph.
T1
T2
T3
BA
C
Then there is thefollowing temporalcycle in the schedule:U1(A)àL2(A) L2(A)àU2(B) why?
![Page 53: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/53.jpg)
TWO PHASE LOCKING (2PL)
53
Theorem: 2PL ensures conflict serializability
Proof. Suppose not: thenthere exists a cyclein the precedence graph.
T1
T2
T3
BA
C
Then there is thefollowing temporalcycle in the schedule:U1(A)àL2(A)L2(A)àU2(B)U2(B)àL3(B) why?
![Page 54: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/54.jpg)
TWO PHASE LOCKING (2PL)
Theorem: 2PL ensures conflict serializability
Proof. Suppose not: thenthere exists a cyclein the precedence graph.
T1
T2
T3
BA
C
Then there is thefollowing temporalcycle in the schedule:U1(A)àL2(A)L2(A)àU2(B)U2(B)àL3(B)
......etc.....
![Page 55: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/55.jpg)
TWO PHASE LOCKING (2PL)
55
Theorem: 2PL ensures conflict serializability
Proof. Suppose not: thenthere exists a cyclein the precedence graph.
T1
T2
T3
BA
C
Then there is thefollowing temporalcycle in the schedule:U1(A)àL2(A)L2(A)àU2(B)U2(B)àL3(B)L3(B)àU3(C)U3(C)àL1(C)L1(C)àU1(A)
Cycle in time:Contradiction
![Page 56: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/56.jpg)
A NEW PROBLEM: NON-RECOVERABLE SCHEDULE
T1 T2L1(A); L1(B); READ(A)A :=A+100WRITE(A); U1(A)
L2(A); READ(A)A := A*2WRITE(A); L2(B); BLOCKED…
READ(B)B :=B+100WRITE(B); U1(B);
…GRANTED; READ(B)B := B*2WRITE(B); U2(A); U2(B); Commit
Rollback
![Page 57: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/57.jpg)
A NEW PROBLEM: NON-RECOVERABLE SCHEDULE
T1 T2L1(A); L1(B); READ(A)A :=A+100WRITE(A); U1(A)
L2(A); READ(A)A := A*2WRITE(A); L2(B); BLOCKED…
READ(B)B :=B+100WRITE(B); U1(B);
…GRANTED; READ(B)B := B*2WRITE(B); U2(A); U2(B); Commit
RollbackElements A, B writtenby T1 are restoredto their original value.
![Page 58: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/58.jpg)
A NEW PROBLEM: NON-RECOVERABLE SCHEDULE
T1 T2L1(A); L1(B); READ(A)A :=A+100WRITE(A); U1(A)
L2(A); READ(A)A := A*2WRITE(A); L2(B); BLOCKED…
READ(B)B :=B+100WRITE(B); U1(B);
…GRANTED; READ(B)B := B*2WRITE(B); U2(A); U2(B); Commit
RollbackElements A, B writtenby T1 are restoredto their original value.
Dirty reads ofA, B lead toincorrect writes.
![Page 59: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/59.jpg)
A NEW PROBLEM: NON-RECOVERABLE SCHEDULE
T1 T2L1(A); L1(B); READ(A)A :=A+100WRITE(A); U1(A)
L2(A); READ(A)A := A*2WRITE(A); L2(B); BLOCKED…
READ(B)B :=B+100WRITE(B); U1(B);
…GRANTED; READ(B)B := B*2WRITE(B); U2(A); U2(B); Commit
RollbackElements A, B writtenby T1 are restoredto their original value. Can no longer undo!
Dirty reads ofA, B lead toincorrect writes.
![Page 60: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/60.jpg)
STRICT 2PL
All locks are held until commit/abort:All unlocks are done together with commit/abort.
The Strict 2PL rule:
With strict 2PL, we will get schedules thatare both conflict-serializable and recoverable
![Page 61: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/61.jpg)
STRICT 2PLT1 T2L1(A); READ(A)A :=A+100WRITE(A);
L2(A); BLOCKED…L1(B); READ(B)B :=B+100WRITE(B); Rollback & U1(A);U1(B);
…GRANTED; READ(A)A := A*2WRITE(A); L2(B); READ(B)B := B*2WRITE(B);
Commit & U2(A); U2(B);
![Page 62: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/62.jpg)
STRICT 2PLLock-based systems always use strict 2PLEasy to implement:
• Before a transaction reads or writes an element A, insert an L(A)
• When the transaction commits/aborts, then release all locksEnsures both conflict serializability and recoverability
![Page 63: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/63.jpg)
ANOTHER PROBLEM: DEADLOCKST1: R(A), W(B)T2: R(B), W(A)
T1 holds the lock on A, waits for BT2 holds the lock on B, waits for A
This is a deadlock!
![Page 64: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/64.jpg)
ANOTHER PROBLEM: DEADLOCKSTo detect a deadlocks, search for a cycle in the waits-for graph:T1 waits for a lock held by T2;T2 waits for a lock held by T3;. . .Tn waits for a lock held by T1
Relatively expensive: check periodically, if deadlock is found, then abort one TXN;re-check for deadlock more often (why?)
![Page 65: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/65.jpg)
LOCK MODES
S = shared lock (for READ)X = exclusive lock (for WRITE)
None S XNone
SX
Lock compatibility matrix:
![Page 66: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/66.jpg)
LOCK MODES
S = shared lock (for READ)X = exclusive lock (for WRITE)
None S XNone ✔ ✔ ✔
S ✔ ✔ ✖
X ✔ ✖ ✖
Lock compatibility matrix:
![Page 67: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/67.jpg)
LOCK GRANULARITY
Fine granularity locking (e.g., tuples)• High concurrency• High overhead in managing locks• E.g., SQL Server
Coarse grain locking (e.g., tables, entire database)• Many false conflicts• Less overhead in managing locks• E.g., SQL Lite
Solution: lock escalation changes granularity as needed
![Page 68: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/68.jpg)
LOCK PERFORMANCETh
roug
hput
(TPS
)
# Active Transactions
thrashing
Why ?
TPS =Transactionsper second
To avoid, use admission control
![Page 69: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/69.jpg)
PHANTOM PROBLEMSo far we have assumed the database to be a static collection of elements (=tuples)
If tuples are inserted/deleted then the phantom problem appears
![Page 70: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/70.jpg)
PHANTOM PROBLEM
Is this schedule serializable ?
T1 T2SELECT *FROM ProductWHERE color=‘blue’
INSERT INTO Product(name, color)VALUES (‘A3’,’blue’)
SELECT *FROM ProductWHERE color=‘blue’
Suppose there are two blue products, A1, A2:
![Page 71: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/71.jpg)
PHANTOM PROBLEM
R1(A1);R1(A2);W2(A3);R1(A1);R1(A2);R1(A3)
T1 T2SELECT *FROM ProductWHERE color=‘blue’
INSERT INTO Product(name, color)VALUES (‘A3’,’blue’)
SELECT *FROM ProductWHERE color=‘blue’
Suppose there are two blue products, A1, A2:
![Page 72: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/72.jpg)
W2(A3);R1(A1);R1(A2);R1(A1);R1(A2);R1(A3)
PHANTOM PROBLEM
R1(A1);R1(A2);W2(A3);R1(A1);R1(A2);R1(A3)
T1 T2SELECT *FROM ProductWHERE color=‘blue’
INSERT INTO Product(name, color)VALUES (‘A3’,’blue’)
SELECT *FROM ProductWHERE color=‘blue’
Suppose there are two blue products, A1, A2:
![Page 73: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/73.jpg)
PHANTOM PROBLEMA “phantom” is a tuple that is invisible during part of a transaction execution but not invisible during the entire execution
In our example:• T1: reads list of products• T2: inserts a new product• T1: re-reads: a new product appears !
![Page 74: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/74.jpg)
DEALING WITH PHANTOMSLock the entire tableLock the index entry for ‘blue’
• If index is availableOr use predicate locks
• A lock on an arbitrary predicate
Dealing with phantoms is expensive !
![Page 75: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/75.jpg)
SUMMARY OF SERIALIZABILITYSerializable schedule = equivalent to a serial schedule(strict) 2PL guarantees conflict serializability
• What is the difference?Static database:
• Conflict serializability implies serializabilityDynamic database:
• This no longer holds
![Page 76: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/76.jpg)
ISOLATION LEVELS IN SQL
1. “Dirty reads”SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
2. “Committed reads”SET TRANSACTION ISOLATION LEVEL READ COMMITTED
3. “Repeatable reads”SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
4. Serializable transactionsSET TRANSACTION ISOLATION LEVEL SERIALIZABLE
ACID
![Page 77: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/77.jpg)
1. ISOLATION LEVEL: DIRTY READS“Long duration” WRITE locks
• Strict 2PLNo READ locks
• Read-only transactions are never delayed
Possible problems: dirty and inconsistent reads
![Page 78: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/78.jpg)
2. ISOLATION LEVEL: READ COMMITTED “Long duration” WRITE locks
• Strict 2PL“Short duration” READ locks
• Only acquire lock while reading (not 2PL)
Unrepeatable reads:When reading same element twice, may get two different values
![Page 79: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/79.jpg)
3. ISOLATION LEVEL: REPEATABLE READ “Long duration” WRITE locks
• Strict 2PL“Long duration” READ locks
• Strict 2PL
This is not serializable yet !!!
Why ?
![Page 80: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/80.jpg)
4. ISOLATION LEVEL SERIALIZABLE“Long duration” WRITE locks
• Strict 2PL“Long duration” READ locks
• Strict 2PLPredicate locking
• To deal with phantoms
![Page 81: transactions - University of Washington](https://reader031.fdocuments.in/reader031/viewer/2022021405/6209162cdf21dc03d5599112/html5/thumbnails/81.jpg)
BEWARE!In commercial DBMSs:Default level is often NOT serializableDefault level differs between DBMSsSome engines support subset of levels!Serializable may not be exactly ACID
• Locking ensures isolation, not atomicityAlso, some DBMSs do NOT use locking and different isolation levels can lead to different pbsBottom line: Read the doc for your DBMS!