Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu.

Similar presentations


Presentation on theme: "Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu."— Presentation transcript:

1 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu

2 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke2 Transactions v Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are frequent, and relatively slow, it is important to keep the cpu humming by working on several user programs concurrently. v A user’s program may carry out many operations on the data retrieved from the database, but the DBMS is only concerned about what data is read/written from/to the database. v A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.

3 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke3 Review: The ACID properties v A v A tomicity: All actions in the Xact happen, or none happen. v C v C onsistency: If each Xact is consistent, and the DB starts consistent, it ends up consistent. v I v I solation: Execution of one Xact is isolated from that of other Xacts. v D v D urability: If a Xact commits, its effects persist. v The Recovery Manager guarantees Atomicity & Durability.

4 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke4 Atomicity of Transactions v A transaction might commit after completing all its actions, or it could abort (or be aborted by the DBMS) after executing some actions. v A very important property guaranteed by the DBMS for all transactions is that they are atomic. That is, a user can think of a Xact as always executing all its actions in one step, or not executing any actions at all. – DBMS logs all actions so that it can undo the actions of aborted transactions.

5 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke5 Example v Consider two transactions ( Xacts ): T1:BEGIN A=A+100, B=B-100 END T2:BEGIN A=1.06*A, B=1.06*B END v Intuitively, the first transaction is transferring $100 from B’s account to A’s account. The second is crediting both accounts with a 6% interest payment. v There is no guarantee that T1 will execute before T2 or vice-versa, if both are submitted together. However, the net effect must be equivalent to these two transactions running serially in some order.

6 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke6 Example (Contd.) v Consider a possible interleaving ( schedule ): T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B v This is OK. But what about: T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B v The DBMS’s view of the second schedule: T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B)

7 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke7 Scheduling Transactions v Serial schedule: Schedule that does not interleave the actions of different transactions. v Equivalent schedules : For any database state, the effect (on the set of objects in the database) of executing the first schedule is identical to the effect of executing the second schedule. v Serializable schedule : A schedule that is equivalent to some serial execution of the transactions. (Note: If each transaction preserves consistency, every serializable schedule preserves consistency. )

8 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke8 Anomalies with Interleaved Execution v Reading Uncommitted Data (WR Conflicts, “dirty reads”): v Unrepeatable Reads (RW Conflicts): T1: R(A), W(A), R(B), W(B), Abort T2:R(A), W(A), C T1:R(A), R(A), W(A), C T2:R(A), W(A), C

9 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke9 Anomalies (Continued) v Overwriting Uncommitted Data (WW Conflicts): T1:W(A), W(B), C T2:W(A), W(B), C

10 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke10 Some Concepts v A Serializable schedule over a set S of transactions is a schedule whose effect on any consistent database instance is guaranteed to be identical to that of some complete serial schedule over the set of committed transactions in S. –Example –Serializable, unrecoverable –Cascading aborts v A recoverable schedule is one in which transactions commit only after all transactions whose changes they read commit. T1 R(A) W(A) Abort T2 R(A) W(A) R(B) W(B) Commit

11 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke11 Lock-Based Concurrency Control v To ensure only serializable, recoverable schedules are allowed v Strict Two-phase Locking (Strict 2PL) Protocol : – Each Xact must obtain a S ( shared ) lock on object before reading, and an X ( exclusive ) lock on object before writing. – All locks held by a transaction are released when the transaction completes – If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. v Strict 2PL allows only serializable schedules.

12 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke12 Conflict Serializable Schedules v Two schedules are conflict equivalent if: – Involve the same actions of the same transactions – Every pair of conflicting actions of two committed transactions is ordered the same way u two actions conflict if they operate on the same data object and at least one of them is write. v Schedule S is conflict serializable if S is conflict equivalent to some serial schedule

13 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke13 Example v A schedule that is not conflict serializable: v The cycle in the graph reveals the problem. The output of T1 depends on T2, and vice- versa. T1: R(A), W(A), R(B), W(B) T2: R(B), W(B), R(A), W(A) T1T2 A B Precedence graph

14 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke14 Dependency Graph v Precedence graph : One node per Xact; edge from Ti to Tj if an action of Ti precedes and conflicts with one of Tj’s actions v Theorem: Schedule is conflict serializable if and only if its dependency graph is acyclic

15 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke15 Strict 2PL v Strict Two-phase Locking (Strict 2PL) Protocol : – Each Xact must obtain a S ( shared ) lock on object before reading, and an X ( exclusive ) lock on object before writing. – All locks held by a transaction are released when the transaction completes – If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. v Strict 2PL allows only schedules whose precedence graph is acyclic

16 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke16 Strict 2PL(cont’d) v Strict 2PL is too strict for serializability v Strict schedules are recoverable, no need cascading aborts v A DBMS must be able to ensure that only serializable, recoverable schedules are allowed, and that no actions of committed transactions are lost while undoing aborted transactions. T1: R(A), W(A), R(B), W(B), … C T2: R(A), W(A), R(B), W(B)

17 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke17 Two-Phase Locking (2PL) v Two-Phase Locking Protocol – Each Xact must obtain a S ( shared ) lock on object before reading, and an X ( exclusive ) lock on object before writing. – If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. – A transaction can not request additional locks once it releases any locks.

18 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke18 Lock Management v Lock and unlock requests are handled by the lock manager v Lock table entry: –Number of transactions currently holding a lock –Type of lock held(shared or exclusive) –Pointer to queue of lock requests v Locking and unlocking have to be atomic operations v Lock upgrade: transaction that holds a shared lock can be upgraded to hold an exclusive lock

19 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke19 Deadlocks v Deadlock: Cycle of transactions waiting for locks to be released by each other. v Two ways of dealing with deadlocks: – Deadlock prevention – Deadlock detection

20 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke20 Deadlock Prevention v Assign priorities based on timestamps. Assume Ti wants a lock that Tj holds. Two policies are possible: – Wait-Die: It Ti has higher priority, Ti waits for Tj; otherwise Ti aborts – Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti waits v If a transaction re-starts, make sure it has its original timestamp

21 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke21 Deadlock Detection v Create a waits-for graph: – Nodes are transactions – There is an edge from Ti to Tj if Ti is waiting for Tj to release a lock v Periodically check for cycles in the waits-for graph

22 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke22 Deadlock Detection(cont’d) v Example –T1: S(A), R(A) S(B) –T2: X(B),W(B) X(C) –T3: S(C),R(C) X(A) –T4: X(B)

23 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke23 Aborting a Transaction v If a transaction Ti is aborted, all its actions have to be undone. Not only that, if Tj reads an object last written by Ti, Tj must be aborted as well! v Most systems avoid such cascading aborts by releasing a transaction’s locks only at commit time. – If Ti writes an object, Tj can read this only after Ti commits. v In order to undo the actions of an aborted transaction, the DBMS maintains a log in which every write is recorded. This mechanism is also used to recover from system crashes: all active Xacts at the time of the crash are aborted when the system comes back up.

24 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke24 The Log v The following actions are recorded in the log: – Ti writes an object : the old value and the new value. u Log record must go to disk before the changed page! – Ti commits/aborts : a log record indicating this action. v Log records are chained together by Xact id, so it’s easy to undo a specific Xact. v Log is often duplexed and archived on stable storage. v All log related activities (and in fact, all CC related activities such as lock/unlock, dealing with deadlocks etc.) are handled transparently by the DBMS.

25 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke25 Motivation v Atomicity: –Transactions may abort (“Rollback”). v Durability: –What if DBMS stops running? (Causes?) crash! v Desired Behavior after system restarts: –T1, T2 & T3 should be durable. –T4 & T5 should be aborted (effects not seen). T1 T2 T3 T4 T5

26 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke26 Assumptions v Concurrency control is in effect. –Strict 2PL, in particular. v Updates are happening “in place”. –i.e. data is overwritten on (deleted from) the disk. v A simple scheme to guarantee Atomicity & Durability?

27 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke27 Handling the Buffer Pool v Force every write to disk? –Poor response time. –But provides durability. v Steal buffer-pool frames from uncommited Xacts? –If not, poor throughput. –If so, how can we ensure atomicity? Force No Force No Steal Steal Trivial Desired

28 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke28 Write-Ahead Logging (WAL) v The Write-Ahead Logging Protocol:  Must force the log record for an update before the corresponding data page gets to disk. ‚ Must write all log records for a Xact before commit. v #1 guarantees Atomicity. v #2 guarantees Durability. v Exactly how is logging (and recovery!) done? –We’ll study the ARIES algorithms.

29 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke29 Recovering From a Crash v There are 3 phases in the Aries recovery algorithm: – Analysis : Scan the log forward (from the most recent checkpoint ) to identify all Xacts that were active, and all dirty pages in the buffer pool at the time of the crash. – Redo : Redoes all updates to dirty pages in the buffer pool, as needed, to ensure that all logged updates are in fact carried out and written to disk. – Undo : The writes of all Xacts that were active at the crash are undone (by restoring the before value of the update, which is in the log record for the update), working backwards in the log. (Some care must be taken to handle the case of a crash occurring during the recovery process!)

30 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke30 Why do we need redo and undo? v Buffer page and disk –Steal: the changes made to an object O in the buffer pool by a T can be written to disk before T commits. –Force: when a T commits, we must ensure that all changes it has made to objects in the buffer pool are immediately forced to disk. v Which strategy we should choose? –No-steal, force (no undo, no redo) –No-steal, no-force (no undo, redo) –Steal, force (undo, no redo) –Steal, no-force (undo, redo)

31 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke31 Summary v Concurrency control and recovery are among the most important functions provided by a DBMS. v Users need not worry about concurrency. – System automatically inserts lock/unlock requests and schedules actions of different Xacts in such a way as to ensure that the resulting execution is equivalent to executing the Xacts one after the other in some order. v Write-ahead logging (WAL) is used to undo the actions of aborted transactions and to restore the system to a consistent state after a crash. – Consistent state : Only the effects of commited Xacts seen.

32 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke32 Properties of Trans---ACID atomicity -- a T is treated as a unit of operation. All-or- nothing property consistency --- correctness T does not overwrite dirty data of other Ts. T does not commit any write until it commits all its writes T does not read dirty data from other Ts. Other Ts do not dirty any data read by T before T completes isolation Read uncomitted --- dirty read, fuzzy read, phantom Read Committed --- fuzzy read, phantom Repeatable Read --- phantom Anomaly Serializable --- none durability --- once T commits, its results are permanent d0 d1 d2 d3

33 Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke33 Phenomena in SQL92 Dirty data refers to data values that have been updated by a T prior to its commitment Dirty Read (WR conflict) …, w1(x),…R2(x),…,C1 (or A1), …C2(or A2) or …, w1(x),…R2(x),…,C2 (or A2), …C1(or A1) Fuzzy Read (unrepeatable reads RW conflict) …, R1(x),…,W2(x),…,R1(x), …,C1(or A1),…,C2(or A2) or …, R1(x),…,W2(x),…,R1(x), …, C2(or A2),…,C1(or A1) Phantom …, R1(P),…,W2(yinP), …, R1(P),…, C1(or A1),…,C2(or A2) or ….,R1(P),…,W2(yinP), …,R1(P),…,C2(or A2),…,C1(or A1)


Download ppt "Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu."

Similar presentations


Ads by Google