Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 15 Transaction Management Outline Transaction basics Concurrency control Recovery management Transaction design issues Workflow management McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Transaction Definition Supports daily operations of an organization Collection of database operations Reliably and efficiently processed as one unit of work No lost data – Interference among multiple users – Failures McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Airline Transaction Example START TRANSACTION Display greeting Get reservation preferences from user SELECT departure and return flight records If reservation is acceptable then UPDATE seats remaining of departure flight record UPDATE seats remaining of return flight record INSERT reservation record Print ticket if requested End If On Error: ROLLBACK COMMIT McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Transaction Properties Atomic: all or nothing Consistent: database must be consistent before and after a transaction Isolated: no unwanted interference from other users Durable: database changes are permanent after the transaction completes McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Transaction Processing Services Concurrency control Recovery management Service characteristics – Transparent – Consume significant resources – Significant cost component McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Concurrency Control Problem definition Concurrency control problems Concurrency control tools McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Concurrency Control Problem Objective: – Maximize work performed – Throughput: number of transactions processed per unit time Constraint: – No interference: serial effect – Interference occurs on commonly manipulated data known as hot spots McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Lost Update Problem Transaction A Time Transaction B Read SR (10) T1 T2 Read SR (10) If SR > 0 then T3 SR = SR -1 T4 If SR > 0 then SR = SR -1 Write SR (9) T5 T6 Write SR (9) McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Uncommitted Dependency Problem Transaction A Read SR (10) SR = SR - 1 Write SR (9) ROLLBACK McGraw-Hill/Irwin Time Transaction B T1 T2 T3 T4 Read SR (9) T5 © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Inconsistent Retrieval Problems Interference causes inconsistency among multiple retrievals of a subset of data Incorrect summary Phantom read Non repeatable read McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Incorrect Summary Problem Transaction A Read SR1 (10) SR1 = SR1 - 1 Write SR1 (9) Read SR2 (5) SR2 = SR2 - 1 Write SR2 (4) McGraw-Hill/Irwin Time T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 Transaction B Read SR1 (9) Sum = Sum + SR1 Read SR2 (5) Sum = Sum + SR2 © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Locking Fundamentals Fundamental tool of concurrency control Obtain lock before accessing an item Wait if a conflicting lock is held – Shared lock: conflicts with exclusive locks – Exclusive lock: conflicts with all other kinds of locks Concurrency control manager maintains the lock table McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Locking Granularity Database Table Index Page Row Column McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Deadlock (Mutual Waiting) Transaction A XLock SR1 Time Transaction B T1 T2 XLock SR2 (wait) T3 T4 McGraw-Hill/Irwin XLock SR2 XLock SR1 (wait) © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Deadlock Resolution Detection – Can involve significant overhead – Not widely used Timeout – Waiting limit – Can abort transactions that are not deadlocked – Widely used although timeout interval is difficult to determine McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Two Phase Locking (2PL) Protocol to prevent lost update problems All transactions must follow Conditions – Obtain lock before accessing item – Wait if a conflicting lock is held – Cannot obtain new locks after releasing locks McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Locks held 2PL Implementation BOT McGraw-Hill/Irwin as h p g in w o Gr e Time Shrinking phase EOT © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Optimistic Approaches Assumes conflicts are rare No locks Check for conflicts – After each read and write – At end of transaction Evaluation – Less overhead – More variability McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Recovery Management Device characteristics and failure types Recovery tools Recovery processes McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Storage Device Basics Volatile: loses state after a shutdown Nonvolatile: retains state after a shutdown Nonvolatile is more reliable than volatile but failures can cause loss of data Use multiple levels and redundant levels of nonvolatile storage for valuable data McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Failure Types Local – Detected and abnormal termination – Limited to a single transaction Operating System – Affects all active transactions – Less common than local failures Device – Affects all active and past transactions – Least common McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Transaction Log History of database changes Large storage overhead Operations – Undo: revert to previous state – Redo: reestablish a new state Fundamental tool of recovery management McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Transaction Log Example LSN TransNo Action Time Table Row 1 2 3 4 101001 101001 101001 101001 START UPDATE UPDATE INSERT 5 101001 COMMIT 10:33 McGraw-Hill/Irwin 10:29 10:30 Acct 10:30 Acct 10:32 Hist Column Old New 10001 AcctBal 100 200 15147 AcctBal 500 400 25045 * <1002, 500, …> © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Checkpoints Reduces restart work but adds overhead – Checkpoint log record – Write log buffers and database buffers Checkpoint interval: time between checkpoints Types of checkpoints – Cache consistent – Fuzzy McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Other Recovery Tools Force writing – Checkpoint time – End of transaction Database backup – Complete – Incremental McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Recovery from a Media Failure Restore database from the most recent backup Redo all committed transactions since the most recent backup Restart active transactions McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Recovery Timeline Checkpoint Failure Time T1 T2 T3 T4 T5 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Recovery Processes Depend on timing of database writes Immediate update approach: – Before commit – Log records written first (write-ahead log protocol) Deferred update approach – After commit – Undo operations not needed McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Immediate Update Recovery Class T1 T2 T3 T4 T5 McGraw-Hill/Irwin Description Finished before CP Started before CP; finished before failure Started after CP; finished before failure Started before CP; not yet finished Started after CP; not yet finished Restart Work None Redo forward from checkpoint Redo forward from checkpoint Undo backwards from most recent log record Undo backwards from most recent log record © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Deferred Update Recovery Class Description T1 Finished before CP T2 Started before CP; finished before failure T3 Started after CP; finished before failure T4 Started before CP; not yet finished T5 Started after CP; not yet finished McGraw-Hill/Irwin Restart Work None Redo forward from first log record Redo forward from first log record None None © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Transaction Design Issues Transaction boundary Isolation levels Deferred constraint checking Savepoints McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Transaction Boundary Decisions Division of work into transactions Objective: minimize transaction duration Constraint: enforcement of important integrity constraints Transaction boundary decision can affect hot spots McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Registration Form Example McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Transaction Boundary Choices One transaction for the entire form One transaction for the main form and one transaction for all subform records One transaction for the main form and separate transactions for each subform record McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Isolation Levels Degree to which a transaction is separated from the actions of other transactions Balance concurrency control overhead with interference problems Some transactions can tolerate uncommitted dependency and inconsistent retrieval problems Specify using the SET TRANSACTION statement McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. SQL Isolation Levels Level XLocks SLocks PLocks Interference Read uncommitted None None None Uncommitted dependency Read committed Long Short None Repeatable read Long Long Short (S), Long (X) All except uncommitted dependency Phantom reads Serializable Long Long Long McGraw-Hill/Irwin None © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Scholar’s Lost Update Transaction A Obtain S lock on SR Read SR (10) Release S lock on SR If SR > 0 then SR = SR -1 Obtain X lock on SR Write SR (9) Commit McGraw-Hill/Irwin Time Transaction B T1 T2 T3 T4 T5 T6 Obtain S lock on SR Read SR (10) T7 T8 Release S lock on SR If SR > 0 then SR = SR -1 T9 T10 T11 T12 T13 Obtain X lock on SR Write SR (9) © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Integrity Constraint Timing Most constraints checked immediately Can defer constraint checking to EOT SQL SET CONSTRAINTS statement McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Save Points Some transactions have tentative actions SAVEPOINT statement determines intermediate points ROLLBACK to specified save points McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Workflow Management Workflow description Enabling technologies Advanced transaction management McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Workflows Set of tasks to accomplish a business process Human-oriented vs. computer-oriented – Amount of judgment – Amount of automation Task structure vs. task complexity – Relationships among tasks – Difficulty of performing individual tasks McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Enabling Technologies Distributed object management – Many kinds of non traditional data – Data often dispersed in location Workflow modeling – Specification – Simulation – Optimization McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Advanced Transaction Management Conversational transactions Transactions with complex structure Transactions involving legacy systems Compensating transactions More flexible transaction processing McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Summary Transaction: user-defined collection of work DBMSs support ACID properties Knowledge of concurrency control and recovery important for managing databases Transaction design issues are important Transaction processing is an important part of workflow management McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.