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 Database Design, Application Development, and Administration, 5th Edition Copyright © 2011 by Michael V. Mannino. All rights reserved. Outline Transaction basics Concurrency control Recovery management Transaction design issues Workflow management Chapter 15: Transaction Management Slide 2 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 Chapter 15: Transaction Management Slide 3 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 Chapter 15: Transaction Management Slide 4 ATM Transaction Example START TRANSACTION Display greeting Get account number, pin, type, and amount SELECT account number, type, and balance If balance is sufficient then UPDATE account by posting debit UPDATE account by posting debit INSERT history record Display message and dispense cash Print receipt if requested End If On Error: ROLLBACK COMMIT Chapter 15: Transaction Management Slide 5 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 Chapter 15: Transaction Management Slide 6 Transaction Processing Services Concurrency control Recovery management Service characteristics Transparent Consume significant resources Significant cost component Transaction design important Chapter 15: Transaction Management Slide 7 Concurrency Control Problem definition Concurrency control problems Concurrency control tools Chapter 15: Transaction Management Slide 8 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 Chapter 15: Transaction Management Slide 9 Conflicting Actions Conflicts involve one or more common items Same transaction order must be maintained for each common database item No conflicts if just read operations on common data Conflict classification W-W conflict: Wi(A) Wj(A) and Wj(B) Wi(B) R-W conflict: Ri(A) Wj(A) and Rj(B) Wi(B) W-R conflict: Wj(A) Ri(A) and Wi(B) Rj(B) Chapter 15: Transaction Management Slide 10 Conflict Examples Example 1: R2(A), W2(A), R1(A), R1(B), R2(B), W2(B) T1 first: R1(A), R1(B), R2(A), W2(A), R2(B), W2(B) In T1 first, R1(A) precedes W2(A) and R1(B) precedes W2(B) T2 first: R2(A), W2(A), R2(B), W2(B), R1(A), R1(B) In T2 first, W2(A) precedes R1(A) and W2(B) precedes R1(B) Example 2: R2(A), W2(A), R1(A), R2(B), W2(B), R1(B) Serializable as the ordering of operations on common items is equivalent to T2 first Conflicting actions occur in the same order as T2 first Chapter 15: Transaction Management Slide 11 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) Chapter 15: Transaction Management Slide 12 Uncommitted Dependency Problem Transaction A Read SR (10) SR = SR - 1 Write SR (9) ROLLBACK Chapter 15: Transaction Management Time Transaction B T1 T2 T3 T4 Read SR (9) T5 Slide 13 Inconsistent Retrieval Problems Interference causes inconsistency among multiple retrievals of a subset of data Incorrect summary Phantom read Non repeatable read Chapter 15: Transaction Management Slide 14 Incorrect Summary Problem Transaction A Read SR1 (10) SR1 = SR1 - 1 Write SR1 (9) Read SR2 (5) SR2 = SR2 - 1 Write SR2 (4) Chapter 15: Transaction Management 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 Slide 15 Other Inconsistent Retrieval Problems Non repeatable read Transaction A executes a query two times. In between the two executions, other transactions change the data so that the second result is different than the first result. Phantom read Transaction A: SELECT COUNT(*) … WHERE Salary > 50000 Transaction B: insert a new row with salary = 55000 Transaction A: reexecute query again with Salary > 50000 Chapter 15: Transaction Management Slide 16 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 Chapter 15: Transaction Management Slide 17 Basic Compatibility Matrix User 2 Requests User 1 Holds S Lock X Lock S Lock Lock granted User 2 waits X Lock User 2 waits User 2 waits Chapter 15: Transaction Management Slide 18 Locking Granularity Database Table Index Page Row Column Chapter 15: Transaction Management Slide 19 Intention Locks Allow transactions to quickly determine if rows (or pages) in a given table have been locked by other transactions Obtain intention lock on larger grained object before smaller grained object Types Intention shared (IS): obtain IS lock on a larger grained object if needing shared locks on some finer grained objects Intention exclusive (IX): obtain IX lock on a larger grained object if needing exclusive locks on some finer grained objects Chapter 15: Transaction Management Slide 20 Extended Compatibility Matrix User 2 Requests User 1 Holds IS lock IX lock SIX lock S lock X lock IS Grant Grant Grant Grant Wait IX Grant Grant Wait Wait Wait SIX Grant Wait Wait Wait Wait S Grant Wait Wait Grant Wait X Wait Wait Wait Wait Wait Chapter 15: Transaction Management Slide 21 Deadlock (Mutual Waiting) Transaction A XLock SR1 Time Transaction B T1 T2 XLock SR2 (wait) T3 T4 Chapter 15: Transaction Management XLock SR2 XLock SR1 (wait) Slide 22 Deadlock Resolution Detection Overhead is reasonable for deadlocks among a small number of transactions Used by enterprise DBMSs Timeout Waiting limit Can abort transactions that are not deadlocked Timeout interval is difficult to determine Chapter 15: Transaction Management Slide 23 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 Chapter 15: Transaction Management Slide 24 Locks held 2PL Implementation e as h p g in w o Gr BOT Chapter 15: Transaction Management Time Shrinking phase EOT Slide 25 Extensions for Locking Granularity Small extension of 2PL Lock from root (coarsest level) to finer levels Release locks in opposite order (finest to coarsest) Locking details Obtain S or IS lock: must hold IS or IX lock on parent Obtain X, IX, or SIX: must hold IX or SIX lock on parent Chapter 15: Transaction Management Slide 26 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 Chapter 15: Transaction Management Slide 27 Recovery Management Device characteristics and failure types Recovery tools Recovery processes Chapter 15: Transaction Management Slide 28 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 Chapter 15: Transaction Management Slide 29 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 Chapter 15: Transaction Management Slide 30 Transaction Log History of database changes Large storage overhead Operations Undo: revert to previous state (write old value) Redo: use new state (write new value) Fundamental tool of recovery management Chapter 15: Transaction Management Slide 31 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 Chapter 15: Transaction Management 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, …> Slide 32 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 Incremental Chapter 15: Transaction Management Slide 33 Cache Consistent Checkpoint Traditional checkpoint approach Longest checkpoint time but smallest recovery time Actions No new transactions can start and existing transactions must stop. Write all log buffers to disk. Write all dirty database pages to disk. Write checkpoint record to the log file. Chapter 15: Transaction Management Slide 34 Fuzzy Checkpoint Reduced checkpoint time but increased recovery time Actions No new transactions can start and existing transactions must stop. Write all log buffers to disk. Write all dirty database pages older than the previous checkpoint to disk: fewer disk pages than cache consistent checkpoint. Write checkpoint record to the log file. Record the set of log pages that are dirty since the previous checkpoint Chapter 15: Transaction Management Slide 35 Incremental Checkpoint Smallest checkpoint time but largest recovery time Oracle approach Actions No new transactions can start and existing transactions must stop. Write all log buffers to disk. Write log position of the oldest dirty database page: provides a starting point for recovery Control amount of restart work by the frequency of writing dirty database pages Chapter 15: Transaction Management Slide 36 Comparison of Checkpoint Types Log Pages All Dirty DB Pages Cache Consistent Log Pages Older Dirty DB Pages Fuzzy Log Pages Log Position Incremental Chapter 15: Transaction Management Slide 37 Other Recovery Tools Force writing Checkpoint time End of transaction Database backup Complete Incremental Chapter 15: Transaction Management Slide 38 Recovery from a Media Failure Restore database from the most recent backup Redo all committed transactions since the most recent backup Restart active transactions Chapter 15: Transaction Management Slide 39 Recovery Timeline Checkpoint Failure Time T1 T2 T3 T4 T5 Chapter 15: Transaction Management Slide 40 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 Chapter 15: Transaction Management Slide 41 Immediate Update Recovery Class T1 T2 T3 T4 T5 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 Chapter 15: Transaction Management 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 Slide 42 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 Chapter 15: Transaction Management Restart Work None Redo forward from first log record Redo forward from first log record None None Slide 43 Recovery Examples Chapter 15 Recovery Example document Transaction log and failure time Read log backwards starting with the last log record Immediate update method Deferred update method Chapter 15: Transaction Management Slide 44 Oracle Recovery Features Incremental checkpoints Immediate update approach Mean Time to Recover (MTTR) parameter MTTR advisor Dynamic dictionary views to monitor recovery state Flashback recovery Chapter 15: Transaction Management Slide 45 Oracle Recovery Parameters Tradeoff: checkpoint overhead versus recovery time Parameter choice Log checkpoint timeout: number of seconds Log checkpoint interval: number of redo blocks MTTR target: expected mean time to recover in seconds Set either the MTTR target parameter or the checkpoint timeout and interval parameters MTTR advisor executes transaction loads with different parameter MTTR values. Chapter 15: Transaction Management Slide 46 Dynamic Recovery View (V$Instance_Recovery) Monitors the mechanisms available to users to limit recovery I/O Columns Estimated number of data blocks to be processed during recovery Estimated and current number of redo blocks required for recovery Target and estimated MTTR Optimal log file size Chapter 15: Transaction Management Slide 47 Flashback Recovery Flash database to store old versions Historical queries Specify past table states using timestamps Insert data into current table using timestamp query Alternative recovery method Fast recovery of individual tables to a timestamp Useful for short-term restoration: 2 to 7 days Separate flashback logs Chapter 15: Transaction Management Slide 48 Transaction Design Issues Transaction boundary Isolation levels Deferred constraint checking Savepoints Chapter 15: Transaction Management Slide 49 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 Chapter 15: Transaction Management Slide 50 Registration Form Example Chapter 15: Transaction Management Slide 51 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 Chapter 15: Transaction Management Slide 52 Avoiding User Interaction Time Avoid to increase throughput Possible side effects: user confusion due to database changes Balance increase in throughput with occurrences of side effects Most situations increase in throughput more important than possible user confusion Chapter 15: Transaction Management Slide 53 Revised Airline Reservation Example Get reservation preferences SELECT departure and return flight rows if reservation is acceptable then START TRANSACTION UPDATE seats remaining of departure flight row UPDATE seats remaining of return flight row INSERT reservation row End If On Error: ROLLBACK COMMIT Send receipt to customer Chapter 15: Transaction Management Slide 54 Transaction Design Example Tennis reservations Reserve court 7 days in advance Choose date, start time, duration, court, and partner(s) Subject to partner availability Predictable hot spots Reservation table: date, start time, duration, court, player1, player2, player3, player4 Wait time for partner selection after date, time, court, and duration selected Chapter 15: Transaction Management Slide 55 Tennis Reservation Calendar Chapter 15: Transaction Management Slide 56 Tennis Reservation Selection Chapter 15: Transaction Management Slide 57 Choose Partner(s) Chapter 15: Transaction Management Slide 58 Solutions for Transaction Design Minimal duration Transaction begins after final selection (date, time, court, duration, and partners) No wait time for transaction duration Reservation may not succeed after selection Longer duration with wait time Transaction begins if selection of date, time, court, and duration are not already selected Locks must be held until wait time limit Reservation will succeed after partner selection Chapter 15: Transaction Management Slide 59 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 Chapter 15: Transaction Management Slide 60 SQL Isolation Levels Level XLocks SLocks PLocks Interference Read uncommitted None None None Uncommitted dependency Read committed Long Short None All except uncommitted dependency Repeatable read Long Long Short (S), Long (X) Phantom reads Serializable Long Long Long None Chapter 15: Transaction Management Slide 61 Scholar’s Lost Update Transaction A Time Obtain S lock on SR T1 Read SR (10) T2 Release S lock on SR T3 If SR > 0 then SR = SR -1 T4 T5 Obtain S lock on SR T6 Read SR (10) T7 Release S lock on SR T8 If SR > 0 then SR = SR -1 Obtain X lock on SR T9 Write SR (9) T10 Commit T11 Chapter 15: Transaction Management Transaction B T12 Obtain X lock on SR T13 Write SR (9) Slide 62 Integrity Constraint Timing Most constraints checked immediately Can defer constraint checking to EOT SQL Constraint timing clause for constraints in a CREATE TABLE statement SET CONSTRAINTS statement Chapter 15: Transaction Management Slide 63 Save Points Some transactions have tentative actions SAVEPOINT statement determines intermediate points ROLLBACK to specified save points Safe operations Save point Chapter 15: Transaction Management Tentative operations Rollback to save point Commit Slide 64 Workflow Management Workflow description Enabling technologies Advanced transaction management Chapter 15: Transaction Management Slide 65 Workflow Basics 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 Chapter 15: Transaction Management Slide 66 Workflow Classification Human-oriented Communication support Computer-oriented Transaction support Task complexity Complex Property sale Insurance claim Utility service Bid proposal Travel request Simple Meeting notification Low Chapter 15: Transaction Management Task structure High Slide 67 Enabling Technologies Distributed object management Many kinds of non traditional data Data often dispersed in location Workflow modeling Specification Simulation Optimization Chapter 15: Transaction Management Slide 68 Advanced Transaction Management Conversational transactions Transactions with complex structure Transactions involving legacy systems Compensating transactions More flexible transaction processing Chapter 15: Transaction Management Slide 69 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 Chapter 15: Transaction Management Slide 70