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 v A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes. Concurrent execution of user programs is essential for good DBMS performance. § § v Increasing system throughput (# of completed transactions in any given time) by overlapping I/O and CPU operations Increasing response time (time for completing a transaction) by avoiding short transactions getting stuck behind long ones A user’s program may carry out many (in-memory) operations on the data retrieved from the database, but the DBMS is only concerned about what data is read/written from/to the database. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 2
The ACID Properties Atomicity: Either all actions of the transactions are executed or none at all. v Consistency: Any transaction that starts executing in a consistent database state must leave it in a consistent state upon completion. v Isolation: A transaction is protected from effects of concurrently running transactions. v Durability: Effects of committed transactions must persist and overcome any system failure (system crash/media failure). v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 3
Consistency and Isolation v Users submit transactions, and can think of each transaction as executing by itself. § § v Concurrency is achieved by the DBMS, which interleaves actions (reads/writes of DB objects) of various transactions. The net effect is the same as serially executing the transactions one after the other. 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). Issue: Coping with effects of interleaving transactions (Concurrency control). Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 4
Atomicity and Durability v A transaction might commit after completing all its actions, or it could terminate unsuccessfully: § It could abort (or be aborted by the DBMS) after executing some actions. § The system may crash while Xacts are in progress. § There could be a media failure preventing reads/writes. v Two very important properties guaranteed by the DBMS for all transactions are their atomicity and durability. § Atomicity: DBMS logs all actions so that it can undo the actions of aborted transactions. § Durability: committed actions are written to disk or (in case of a crash) the system must redo actions of committed Xacts which were not yet written to disk. v Issues: Coping with effects of crashes (Recovery). Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 5
Concurrency Control: Example v Consider two transactions (Xacts): T 1: T 2: BEGIN A=A+100, B=B-100 END BEGIN A=1. 06*A, B=1. 06*B END 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 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. v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 6
Example (Contd. ) v Consider a possible interleaving (schedule): T 1: T 2: v B=B-100 A=1. 06*A, B=1. 06*B This is OK. But what about: T 1: T 2: v A=A+100, B=B-100 A=1. 06*A, B=1. 06*B The DBMS’s view of the second schedule: T 1: T 2: R(A), W(A), R(B), W(B) R(A), W(A), R(B), W(B) Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 7
Scheduling Transactions 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. ) v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 8
Anomalies with Interleaved Execution v T 1: T 2: Reading Uncommitted Data (WR Conflicts, “dirty reads”): R(A), W(A), R(B), W(B), Abort R(A), W(A), C Unrepeatable Reads (RW Conflicts): R(A), W(A), C Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 9
Anomalies (Continued) v T 1: T 2: Overwriting Uncommitted Data (WW Conflicts, ‘’lost updates’’): W(A), W(B), C W(A), W(B), C Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 10
Lock-Based Concurrency Control v v A DBMS ensures that only serializable schedules are allowed by using a suitable CC protocol. Strict Two-phase Locking (Strict 2 PL) Protocol: § § § v 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. Strict 2 PL does however allow deadlocks, i. e. cycles of Xacts waiting for locks to be released. A DBMS must either prevent or detect (and resolve) deadlocks. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 11
Aborting a Transaction 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. v § 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 12
Support for Transactions in SQL v v SQL provides users with support to specify Xact-level behavior. A Xact is automatically started with each user SQL statement (except with CONNECT) and terminated with either a COMMIT or a ROLLBACK command. SQL 99 supports transactions with savepoints and chained transactions. § § v v One defines a savepoint and may later selectively roll back to it: e. g. , SAVEPOINT point 1. . . ROLLBACK TO SAVEPOINT point 1. One may commit/rollback and immediately initiate another one: e. g. SELECT * FROM Sailors COMMIT AND CHAIN SELECT * FROM Students ROLLBACK A DBMS can locks DB objects at different granularities: row-level granularity vs. table-level granularity. Row-level granularity is not immune to the phantom phenomenon, i. e. a transaction sees twice a different collection of objects (tuples). The vanishing/new tuples are called phantoms. Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 13
Support for Transactions in SQL (Contd. ) v v SQL provides support for controlling the access mode to DB objects, the diagnostics size, and the isolation level of Xacts. Diagnostics size: # of error conditions recordable by the DBMS Access mode: READ ONLY / WRITE ONLY / READ WRITE Isolation level: controls what other Xacts see of a given Xact. Level READ UNCOMMITTED READ COMMITTED REPTEATABLE READ SERIALIZABLE v Dirty read Unrepeatable read Possible No No Phantom Possible No Example: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 14
Crash Recovery: Stealing / Forcing Pages v v v The recovery manager ensures that Xacts are atomic (by undoing actions of uncommitted Xacts) and durable (by guaranteeing that committed Xacts survive crashes/failures). The transaction manager controls the execution of Xacts by using an appropriate locking protocol. For now, assume page writes are atomic. Alternatives in managing buffers: § § v Steal approach: the changes made to an object O by a transaction T may be written to disk before T commits. This arises when the buffer manager decides to replace the frame containing O by a page (belonging to a different transaction) from disk. Force approach: Force all writes of a committed Xact to disk. Most systems use a steal, no-force approach which is a realistic one: § If any dirty frame is chosen for replacement, the page it contains is written to disk (steal). § Dirty pages in buffer are not forced to disk at commit times of associated Xacts (no-force). Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 15
Crash Recovery: 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! Ti commits/aborts: a log record indicating this action. 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. v Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 16
Crash Recovery: the ARIES Approach v A steal, no-force approach in 3 phases: § § § 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!) Database Management Systems 3 ed, R. Ramakrishnan and J. Gehrke 17
Summary Concurrency control and recovery are among the most important functions provided by a DBMS. v Users need not worry about concurrency. v § 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 18
- Slides: 18