Presentation on theme: "Transaction Program unit that accesses the database"— Presentation transcript:
1 Transaction Program unit that accesses the database Executes atomically (all or nothing) and transforms the DB from one consistent state to anotherSolves problems:integrity constraint violationssystem crashesconcurrent updates
2 Transaction State Diagram ActiveAll statementsexecutedTransaction abortor system crashPartially CommittedFailedUnable toguaranteecompletionNecessaryadjustmentsmade (e.g.,rollback)Able toguaranteecompletionCommittedAborted
3 Logging with Immediate Updates Crash RecoveryLogging with Immediate Updates<T, starts><T, file, field, key, old value, new value><T, commits>Can update anytime after log records with old/newvalues have been written to disk.
4 Crash Recovery Example 1. Assume the system crashes before the commit as follows:<T1, starts><T1, Room, Cost, 1, 90, 95> assume actual update done<T1, Room, Cost, 2, 80, 85> assume log record on diskbut update not done<T1, Room, Cost, 3, 80, 85> assume log record not on disk<T1, commits>Do “undo” for each value record on disk -- overwriteschanged or unchanged values.2. Assume the system crashes after the commit, and all logrecords are on disk but not all updates are done. “Redo”each value record; overwrite changed and unchanged values.
5 Multiple Transactions Recoveryorder seriallyredo those committed in serial orderundo those not committed in reverse serial orderCheckpointsflush all records from bufferdo all updates for committed transactionsadd <checkpoint> to logsubsequently ignore committed transactions before the <checkpoint>
6 Concurrent Updates T1 T2 read # enrolled in CS1 add 1 write # enrolled If 10 are initially enrolled, the DB says 11 after thesetransactions execute, but should say 12.
7 Two-Phase Locking Protocol Strict serial execution (works, but allows no concurrency)2PLP (equivalent to serial execution, but allows concurrency)Phase 1: locking—must not unlockPhase 2: unlocking—must not lockAny attempt by T2to lock or read Nhere fails.T1 T2LX(N)Read(N)Add 1 to NWrite(N)UN(N)T3LX(M)Read(M)Add 1 to MWrite (M)UN(M)T3 can overlapeither T1 or T2or both
8 Simple Locking is Not Enough T1 T2LX(A)A := A+1UN(A)LX(B)A := A*BUN(B)B := BALet A = B = 2 initially.Execute as written:A = 6 B = 4Execute T1 then T2:A = 3 B = 1Execute T2 then T1:A = 5 B = 3Execution as written isnot the same as eitherserial execution.Not 2P
9 DeadlockT1 T2LX(A)LX(B)Try LX(B)Try LX(A)Abort either T1 or T2.
11 Dirty Reads Allow Even More Concurrency Dirty Data: data written by a transaction that has not yet been committed.Dirty Read: a read of dirty data.Problem: can take action on bogus data.The dirty data may be rolled back.May or may not matter (e.g., report of bank balance)Isolation levels control the reading of dirty data (Oracle’s Isolation levels)