Transaction Management Overview Chapter 16 Database Management Systems
Transaction Management Overview Chapter 16 Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 1
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 from and written to the database. v A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 2
Concurrency in a DBMS v v Users submit transactions, and can think of each transaction as executing by itself. § Concurrency is achieved by the DBMS, which interleaves actions (reads/writes of DB objects) of various transactions. § Each transaction must leave the database in a consistent state if the DB is consistent when the transaction begins. • DBMS will enforce some ICs, depending on the ICs declared in CREATE TABLE statements. • Beyond this, the DBMS does not really understand the semantics of the data. (e. g. , it does not understand how the interest on a bank account is computed). Issues: Effect of interleaving transactions, and crashes. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 3
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. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 4
Example v Consider two transactions (Xacts): T 1: T 2: v BEGIN A=A+100, B=B-100 END BEGIN A=1. 06*A, B=1. 06*B END Transfer $100 from B’s account to A’s account Crediting both accounts with a 6% interest payment There is no guarantee that T 1 will execute before T 2 or vice-versa, if both are submitted together. However, the net effect must be equivalent to these two transactions running serially in some order. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 5
Example (Contd. ) v Consider a possible interleaving (schedule): T 1: T 2: A=A+100, A=1. 06*A, B=B-100 B=1. 06*B The $100 transfer amount is given v This is OK. But what about: interest payment twice T 1: A=A+100, B=B-100 T 2: A=1. 06*A, B=1. 06*B v The DBMS’s view of the second schedule: T 1: T 2: R(A), W(A), R(B), W(B) Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke R(B), W(B) 6
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. ) Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 7
Anomalies with Interleaved Execution v Reading Uncommitted Data (WR Conflicts, “dirty reads”): T 1: T 2: v R(A), W(A), C R(B), W(B), Abort Unrepeatable Reads (RW Conflicts): T 1: T 2: R(A), W(A), C Unrepeatable read Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 8
Anomalies (Continued) v Overwriting Uncommitted Data (WW Conflicts): T 1: T 2: W(A), W(B), C Isolation: Even though transactions execute concurrently, it appears to each transaction, T, that other executed either before or after T, but not both. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 9
Isolation Property DBMS ensures that execution of {T 1, . . . , Tn} is equivalent to some serial execution T 1 , . . . Tn. § Before reading/writing an object, a transaction requests a lock on the object, and waits till the DBMS gives it the lock. § All locks are released at the end of the transaction. (Strict 2 PL locking protocol. ) I have the lock I wait T 2 W X R T 1 Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke I can lock now T 2 W I am done X T 1 10
Lock-Based Concurrency Control v Each Xact must obtain an S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. v All locks held by a transaction are released when the transaction completes v If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 11
2 PL Locking Protocol is sufficient and more efficient Number of locks acquired v 2 PL What if I need the lock again before commit ? Strict 2 PL Time v 2 PL offers more concurrency; but it is difficult to implement Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 12
Deadlock I wait for X X W 3 T 2 I have the lock on Y W 1 I have the lock on X R 2 Y T 1 W 4 I wait for Y A solution: T 1 or T 2 is aborted and restarted Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 13
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 (i. e. , Strict 2 PL) § v If Ti writes an object, Tj can read this only after Ti commits. 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. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 14
The Log v The following actions are recorded in the log: § Ti writes an object: the old value and the new value. • Log record must go to disk before the changed page! (WAL protocol) § 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. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 15
Buffer Pool Can the changes made to an object O in the buffer pool be written to disk before the transaction commits ? v v Steal approach: Such write are executed by the replacement policy of the buffer pool (i. e. , another transaction needs to bring in a page) Force approach: When a transaction commits, all the changes it has made to objects in the buffer pool are immediately forced to disk. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke Application Buffer Pool Database 16
Force Approach v Advantage: Recovery after crash is simple § No need to undo the changes of an aborted transaction § No need to redo the changes of a committed transaction v Disadvantage: § There might not be enough pages for a large transaction (e. g. , payroll processing) § It incurs excessive disk I/Os. (e. g. , popular pages will be written to disk frequently by different transactions) Most systems use Steal approach: The in-memory copy of the page can be successively modified by different transactions and written to disk only by the buffer replacement policy (e. g. , LRU). Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 17
Recovering From a Crash 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: Redo 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. (restores the database state to what it was at the time of the crash) § 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. (makes sure the database reflects only the actions of committed transactions) Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 18
Recovery Example Time T 1 T 4 Need REDO T 2 T 3 T 5 Need UNDO Checkpoint Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke Crash 19
Recovering From a Crash Some care must be taken to handle the case of a crash occurring during the recovery process! Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 20
Summary v Concurrency control and recovery are among the most important functions provided by a DBMS. v Users need not worry about concurrency. § v 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. 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. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 21
TEST 2 v v v Time: May 4 th, 2009 10: 00 am – 11: 30 am Materials: Chapters 7, 8, 12, 16, 19 How to prepare for the test § § v Study the Powerpoint slides Review the homework assignments Practice the SQL in the text book For Chapter 7, you do not need to memorize the syntax Grading policy 90 – 100: A Significantly above average effort 80 – 89. 99: B Above average effort 70 – 79. 99: C Average effort (e. g. , studying 70% of materials) 60 – 69. 99: D Need more effort Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 22
- Slides: 22