


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!



Allows your lowest homework grade to be dropped Need one more volunteer (talk to me right after class)
Sign-up will start in a week Early demos are encouraged
3
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;
4
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 )
Good! (^) Bad! Good! (But why?)
Read 400 Read 400 Write 400 – 100 (^) Write 400 – 50
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!
r( X ) and w( X ) conflict w( X ) and r( X ) conflict w( X ) and w( X ) conflict r( X ) and r( X ) do not r/w( X ) and r/w( Y ) do not
E.g., if T 1 .r( A ) precedes T 2 .w( A ), then conceptually, T 1 should precede T 2
T 1 T 2 r( A ) w( A ) r( A ) w( A ) r( B ) r( C ) w( B ) w( C )
T 1 T 2 r( A ) r( A ) w( A ) w( A ) r( B ) r( C ) w( B ) w( C )
T 1
T 2
Good: no cycle
T 1
T 2
Bad: cycle
9
In that serial schedule, transactions are executed in the topological order of the precedence graph You can get to that serial schedule by repeatedly swapping adjacent, non-conflicting operations from different transactions
10
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
Record values before and after each modification: h Ti , X , old_value_of_X , new_value_of_X i
A transaction T (^) i is committed when its commit log record h Ti , commit i is written to disk
Write-ahead logging (WAL): Before X is modified on disk, the log record pertaining to X must be flushed Without WAL, system might crash after X is modified on disk but before its log record is written to disk—no way to undo No force: A transaction can commit even if its modified memory blocks have not be written to disk (since redo information is logged) Steal: Modified memory blocks can be flushed to disk anytime (since undo information is logged)
read( A , a ); a = a – 100; write( A , a ); read( B , b ); b = b + 100; write( B , b );
A = 800 B = 400
700 500
< T 1 , start> < T 1 , A , 800, 700 > < T 1 , B , 400, 500 > < T 1 , commit>
Memory
A = 800 B = 400
Disk Log
Steal: can flush^700 before commit
commit;
500 No force: can flush after commit No restriction on when memory blocks can/should be flushed
21
Naïve approach: Stop accepting new transactions (lame!) Finish all active transactions Take a database dump Now safe to truncate the log
Fuzzy checkpointing Determine S , the set of currently active transactions, and log h begin-checkpoint S i Flush all modified memory blocks at your leisure Log h end-checkpoint begin-checkpoint_location i Between begin and end, continue processing old and new transactions
22
Need to determine U , the set of active transactions at time of crash Scan log backward to find the last end-checkpoint record and follow the pointer to find the corresponding h start-checkpoint S i Initially, let U be S Scan forward from that start-checkpoint to end of the log For a log record h T , start i, add T to U For a log record h T , commit | abort i, remove T from U For a log record h T , X , old , new i, issue write( X , new ) )Basically repeats history!
23
Undo the effects of transactions in U That is, for each log record h T , X , old , new i where T is in U , issue write( X , old ), and log this operation too (part of the repeating-history paradigm) Log h T , abort i when all effects of T have been undone
Each log record stores a pointer to the previous log record for the same transaction; follow the pointer chain during undo
24
Serial schedule: no interleaving Conflict-serializable schedule: no cycles in the precedence graph; equivalent to a serial schedule 2PL: guarantees a conflict-serializable schedule Strict 2PL: also guarantees recoverability
Normal operation: write-ahead logging, no force, steal Recovery: first redo (forward), and then undo (backword)