






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







2
Homework #4 due this Thursday (April 28) Sample solution will be available on Thursday
Project demo period: April 28 – May 1 Remember to email me to sign up for a 30-minute slot
Final exam on Monday, May 2, 2-5pm 3 hours—no time pressure! Open book, open notes Comprehensive, but with emphasis on the second half of the course and materials exercised in homework
Sample final (from last year) available Solution will be available on Thursday
3
Transaction: a sequence of operations with ACID properties Atomicity: TX’s are either completely done or not done at all Consistency: TX’s should leave the database in a consistent state Isolation: TX’s must behave as if they are executed in isolation Durability: Effects of committed TX’s are resilient against failures
SQL transactions -- Begins implicitly SELECT …; UPDATE …; ROLLBACK | COMMIT;
T 1 : read( A ); write( A ); read( B ); write( B ); commit;
T 2 : read( A ); write( A ); read( C ); write( C ); commit;
5
r( A ) w( A ) r( B ) w( B ) r( A ) w( A ) r( C ) w( C )
r( A ) w( A ) r( A ) w( A ) r( B ) r( C ) w( B ) w( C )
r( A ) r( A ) w( A ) w( A ) r( B ) r( C ) w( B ) w( C )
6
T 1 .r( A ), T 1 .w( A ), T 1 .r( B ), T 1 .w( B ), T 2 .r( A ), T 2 .w( A ), T 2 .r( C ), T 2 .w( C ) T 2 .r( A ), T 2 .w( A ), T 2 .r( C ), T 2 .w( C ), T 1 .r( A ), T 1 .w( A ), T 1 .r( B ), T 1 .w( B ) )Isolation achieved by definition!
If a transaction wants to read an object, it must first request a shared lock (S mode) on that object If a transaction wants to modify an object, it must first request an exclusive lock (X mode) on that object Allow one exclusive lock, or multiple shared locks
Mode of lock(s) currently held by other transactions
Mode of the lock requested
Grant the lock?
S X S Yes No X No No Compatibility matrix
11
r( A ) w( A )
r( A ) w( A )
r( B ) w( B )
r( B ) w( B )
lock-X( A )
lock-X( B )
unlock( B )
unlock( A ) lock-X( A )
unlock( A )
unlock( B ) lock-X( B )
Possible schedule under locking But still not conflict-serializable!
T 1
T 2
Read 100 Write 100+
Read 101 Write 101*
Read 100 Write 100*
Read 200 Write 200+
Add 1 to both A and B (preserve A = B )
Multiply both A and B by 2 (preserves A = B )
A ≠ B!
12
Phase 1: obtain locks, phase 2: release locks T 1 T 2 r( A ) w( A )
r( A ) w( A ) r( B ) w( B ) r( B ) w( B )
lock-X( A )
lock-X( B )
unlock( B )
unlock( A ) (^) lock-X( A )
lock-X( B )
Cannot obtain the lock on B until T 1 unlocks
T 1 T 2 r( A ) w( A ) r( A ) w( A ) r( B ) w( B ) r( B ) w( B )
2PL guarantees a conflict-serializable schedule
Schedule is not recoverable if the system crashes right after T 2 commits
T 1 T 2 r( A ) w( A ) r( A ) w( A ) r( B ) w( B ) r( B ) w( B ) … Abort!
14
A writer will block all other readers until the writer commits or aborts
15
T 1 T 2 r( A ) w( A ) r( B ) w( B ) r( B ) r( A ) w( B ) w( A )
T 1
T 2
Deadlock: cycle in the wait-for graph
T 1 is waiting for T 2 T 2 is waiting for T 1
lock-X( A ) lock-X( B )
lock-X( B ) lock-X( A )Deadlock!
Lock: before locking an item, T must acquire intention locks on all ancestors of the item To get S or IS, must hold IS or IX on parent
Mode of lock(s) currently held by other transactions
Mode of the lock requested
Grant the lock?
S X IS IX SIX S Yes Yes X IS Yes Yes Yes Yes IX Yes Yes SIX Yes Compatibility matrix
20
T 1 gets an S lock on R
T 2 gets an SIX lock on R , and then occasionally get an X lock for some rows
T 3 gets an IS lock on R , and then repeatedly gets an S lock on rows it needs to access
21
-- T1: -- T2: SELECT AVG(GPA) FROM Student WHERE age = 10; INSERT INTO Student VALUES(789, ‘Nelson’, 10, 1.0); COMMIT; SELECT AVG(GPA) FROM Student WHERE age = 10; COMMIT;
Use the index on Student ( age ) T 2 locks the index block(s) with entries for age = 10
“Lock” the predicate ( age = 10) Reason with predicates to detect conflicts Expensive to implement
23
24
Use blocking to avoid conflicts Overhead of locking even if contention is low
Assume that most transactions do not conflict Let them execute as much as possible If it turns out that they conflict, abort and restart
Each write creates a new version Each read is directed to an appropriate version Conflicts are detected in a similar manner as timestamp concurrency control
Pro: Reads are never blocked Con: Multiple versions need to be maintained
29
Conflict-serializability 2PL, strict 2PL Deadlocks Multiple-granularity locking Predicate locking and tree locking Overview of other concurrency-control methods
View-serializability Concurrency control for search trees (not the same as multiple-granularity locking and tree locking)