Chương 12: Transaction Management

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. Undo: The writes of all transactions 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!

pptx32 trang | Chia sẻ: vutrong32 | Ngày: 19/10/2018 | Lượt xem: 114 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Chương 12: Transaction Management, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chương 12 Transaction Management110.1 TransactionsConcurrent 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.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.A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.210.2 Transaction ACID PropertiesAtomicEither all actions are carried out or none are.Not worry about incomplete transaction.ConsistencyDBMS assumes that the consistency holds for each transaction. IsolationTransactions are isolated, or protected, from the effects of concurrently scheduling other transactions.DurabilityThe effects of transaction is persist if DBMS informs the user successful execution310.3 Concurrency in a DBMSUsers 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.410.3 Concurrency in a DBMSBeyond 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.510.4 Atomicity of TransactionsA transaction is seen by DBMS as a series or list of actions.Read/Write database object.A transaction might commit after completing all its actions.or it could abort (or be aborted by the DBMS) after executing some actions.610.4 Atomicity of TransactionsTransactions are atomic: a user can think of a transaction 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.7ExampleConsider two transactions (Transactions):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.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.8T1: BEGIN A=A+100, B=B-100 ENDT2: BEGIN A=1.06*A, B=1.06*B ENDExampleConsider a possible interleaving (schedule):This is OK. But what about:The DBMS’s view of the second schedule:9T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*BT1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*BT1: R(A), W(A), R(B), W(B)T2: R(A), W(A), R(B), W(B)10.5 Scheduling TransactionsSchedule: an actual or potential execution sequence.A list of actions from a set of transactions as seen by DBMS.The order in which two actions of a transaction T appear in a schedule must be the same as the order in which they appear in T.1010.5 Scheduling TransactionsClassification:Serial schedule: Schedule that does not interleave the actions of different transactions.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.Serializable schedule: A schedule that is equivalent to some serial execution of the transactions on any consistent database instance.(Note: If each transaction preserves consistency, every serializable schedule preserves consistency. )1110.6 Concurrent Execution of TransactionE.g. Serializable schedule, Equal to the serial schedule T1;T2.E.g. Serializable schedule, Equal to the serial schedule T2;T1.12T1: R(A), W(A), R(B), W(B), CT2: R(A), W(A), R(B), W(B),CT1: R(A), W(A),R(B), W(B), CT2: R(A), W(A), R(B), W(B), C10.6 Concurrent Execution of TransactionWhy concurrent execution?CPU and I/O can work in parallel to increase system throughput.Interleaved execution of a short transaction with a long transaction allows the short transaction to complete quickly, thus prevent stuck transaction or unpredicatable delay in response time.1310.7 Anomalies with Interleaved ExecutionReading Uncommitted Data(WR Conflicts, “dirty reads”): e.g. T1: A+100, B+100, T2: A*1.06, B*1.06Unrepeatable Reads (RW Conflicts): E.g., T1: R(A), check if A >0, decrement, T2: R(A), decrementOverwriting Uncommitted Data (WW Conflicts):14T1: R(A), W(A), R(B), W(B), AbortT2: R(A), W(A), CT1: R(A), R(A), W(A), CT2: R(A), W(A), CT1: W(A), W(B), CT2: W(A), W(B), C10.8 Schedules involving Aborted TransactionsSerializable schedule: 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.Aborted transactions being undone completely– we have to do cascading abort.15T1: R(A),W(A), AbortT2: R(A),W(A),R(B),W(B), Commit10.8 Schedules involving Aborted TransactionsEg: Can we do cascading abort above? We have to abort changes made by T2, but T2 is already committed – we say the above schedule is an Unrecoverable schedule.What we need is Recoverable schedule1610.9 Recoverable SchedulesRecoverable schedule: transactions commit only after all transactions whose changes they read commit.In such a case, we can do cascading abortEg below: Note that T2 cannot commit before T1, therefore when T1 aborts, we can abort T2 as well.17T1: R(A),W(A), AbortT2: R(A),W(A),R(B),W(B), 10.9 Recoverable SchedulesAnother technique: A transaction reads changes only of committed transactions. Advantage of this approach is: the schedule is recoverable, and we will never have to cascade aborts.1810.10 Lock-based Concurrency ControlOnly serializable, recoverable schedules are allowed.No actions of committed transactions are lost while undoing aborted transactions.Lock protocol: a set of rules to be followed by each transaction ( and enforced by DBMS) to ensure that, even though actions of several transactions is interleaved, the net effect is identical to executing all transactions in some serial order.1910.10 Lock-based Concurrency ControlStrict Two-phase Locking (Strict 2PL) Protocol:Rule 1: Each Transaction must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing.Rule 2: All locks held by a transaction are released when the transaction completes.(Non-strict) 2PL Variant: Release locks anytime, but cannot acquire locks after releasing any lock.2010.10 Lock-based Concurrency ControlA transaction that has an exclusive lock can also read the object.A transaction that requests a lock is suspended until the DBMS is able to grant it the requested lock.2110.10 Lock-based Concurrency ControlIn effect, only 'safe' interleaving of transactions are allowed.Two transactions access completely independent parts of database.If accessing same objects, all actions of one of transactions (has the lock) are completed before the other transaction can proceed. Strict 2PL allows only serializable schedules.Additionally, it simplifies transaction aborts(Non-strict) 2PL also allows only serializable schedules, but involves more complex abort processing2210.10 Lock-based Concurrency ControlStrict 2PL23T1: X(A),R(A),W(A),X(B),R(B),W(B),CommitT2: X(A),R(A),W(A),X(B),R(B),W(B), CommitT1: S(A),R(A), X(C),R(C),W(C),CommitT2: S(A),R(A),X(B),R(B),W(B), Commit10.11 DeadlocksDeadlock: Two transactions are waiting for locks from each other. e.g., T1 holds exclusive lock on A, requests an exclusive lock on B and is queued. T2 holds an exclusive lock on B, and request lock on A and queued.Deadlock detectingTimeout mechanism2410.12 Aborting a TransactionIf 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!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.2510.12 Aborting a TransactionIn 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 X acts at the time of the crash are aborted when the system comes back up.2610.13 Performance of lockingLock-based schemes Resolve conflicts between transactions.Two basic mechanisms: blocking and aborting.Blocked transactions hold lock, and force others to wait.Aborting wastes the work done thus far.Deadlock is an extreme instance of blocking. A set of transactions is forever blocked unless one of the deadlocked transactions is aborted.Overhead of locking is primarily from delays due to blocking.2710.14 Crash RecoveryRecovery Manager is responsible for ensuring transaction atomicity and durability.Ensure atomicity by undoing the actions of transactions that do not commit.Ensure durability by making sure that all actions of committed transactions survive system crashes and media failure.Transaction Manager controls execution of transactions.Acquire lock before reading and writing.2810.15 The LogLog: information maintained during normal execution of transactions to enable it to perform its task in the event of a failure.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.2910.15 The LogLog records are chained together by transaction id, so it’s easy to undo a specific transaction.All log related activities are handled transparently by DBMS.In fact, all CC related activities such as lock/unlock, dealing with deadlocks etc.3010.16 Recovering From a CrashCheck pointing: saves information about active transactions and dirty buffer pool pages, also helps reduce the time taken to recover 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 transactions that were active, and all dirty pages in the buffer pool at the time of the crash.3110.16 Recovering From a CrashRedo: 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.Undo: The writes of all transactions that were active at the crash are undoneBy 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!32

Các file đính kèm theo tài liệu này:

  • pptxchuong12_7308.pptx