Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael V. Mannino. All rights reserved. Chapter 15 Transaction.

Similar presentations


Presentation on theme: "Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael V. Mannino. All rights reserved. Chapter 15 Transaction."— Presentation transcript:

1 Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael V. Mannino. All rights reserved. Chapter 15 Transaction Management

2 Slide 2 Chapter 15: Transaction Management Outline  Transaction basics  Concurrency control  Recovery management  Transaction design issues  Workflow management

3 Slide 3 Chapter 15: Transaction Management 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

4 Slide 4 Chapter 15: Transaction Management 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

5 Slide 5 Chapter 15: Transaction Management 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 INSERT history record Display message and dispense cash Print receipt if requested End If On Error: ROLLBACK COMMIT

6 Slide 6 Chapter 15: Transaction Management 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

7 Slide 7 Chapter 15: Transaction Management Transaction Processing Services  Concurrency control  Recovery management  Service characteristics  Transparent  Consume significant resources  Significant cost component  Transaction design important

8 Slide 8 Chapter 15: Transaction Management Concurrency Control  Problem definition  Concurrency control problems  Concurrency control tools

9 Slide 9 Chapter 15: Transaction Management 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

10 Slide 10 Chapter 15: Transaction Management 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: W i (A)  W j (A) and W j (B)  W i (B)  R-W conflict: R i (A)  W j (A) and R j (B)  W i (B)  W-R conflict: W j (A)  R i (A) and W i (B)  R j (B)

11 Slide 11 Chapter 15: Transaction Management Conflict Examples  Example 1: R 2 (A), W 2 (A), R 1 (A), R 1 (B), R 2 (B), W 2 (B)  T 1 first: R 1 (A), R 1 (B), R 2 (A), W 2 (A), R 2 (B), W 2 (B)  In T 1 first, R 1 (A) precedes W 2 (A) and R 1 (B) precedes W 2 (B)  T 2 first: R 2 (A), W 2 (A), R 2 (B), W 2 (B), R 1 (A), R 1 (B)  In T 2 first, W 2 (A) precedes R 1 (A) and W 2 (B) precedes R 1 (B)  Example 2: R 2 (A), W 2 (A), R 1 (A), R 2 (B), W 2 (B), R 1 (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

12 Slide 12 Chapter 15: Transaction Management Lost Update Problem

13 Slide 13 Chapter 15: Transaction Management Uncommitted Dependency Problem

14 Slide 14 Chapter 15: Transaction Management Inconsistent Retrieval Problems  Interference causes inconsistency among multiple retrievals of a subset of data  Incorrect summary  Phantom read  Non repeatable read

15 Slide 15 Chapter 15: Transaction Management Incorrect Summary Problem

16 Slide 16 Chapter 15: Transaction Management 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

17 Slide 17 Chapter 15: Transaction Management 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

18 Slide 18 Chapter 15: Transaction Management Basic Compatibility Matrix User 2 Requests User 1 HoldsS LockX Lock S LockLock grantedUser 2 waits X LockUser 2 waits

19 Slide 19 Chapter 15: Transaction Management Locking Granularity

20 Slide 20 Chapter 15: Transaction Management 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

21 Slide 21 Chapter 15: Transaction Management Extended Compatibility Matrix User 2 Requests User 1 HoldsIS lockIX lockSIX lockS lockX lock ISGrant Wait IXGrant Wait SIXGrantWait SGrantWait GrantWait X

22 Slide 22 Chapter 15: Transaction Management Deadlock (Mutual Waiting)

23 Slide 23 Chapter 15: Transaction Management 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

24 Slide 24 Chapter 15: Transaction Management 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

25 Slide 25 Chapter 15: Transaction Management 2PL Implementation

26 Slide 26 Chapter 15: Transaction Management 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

27 Slide 27 Chapter 15: Transaction Management 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

28 Slide 28 Chapter 15: Transaction Management Recovery Management  Device characteristics and failure types  Recovery tools  Recovery processes

29 Slide 29 Chapter 15: Transaction Management 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

30 Slide 30 Chapter 15: Transaction Management 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

31 Slide 31 Chapter 15: Transaction Management 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

32 Slide 32 Chapter 15: Transaction Management Transaction Log Example

33 Slide 33 Chapter 15: Transaction Management 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

34 Slide 34 Chapter 15: Transaction Management 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.

35 Slide 35 Chapter 15: Transaction Management 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

36 Slide 36 Chapter 15: Transaction Management 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

37 Slide 37 Chapter 15: Transaction Management Comparison of Checkpoint Types Log Pages All Dirty DB Pages Older Dirty DB Pages Log Position Cache Consistent Fuzzy Incremental

38 Slide 38 Chapter 15: Transaction Management Other Recovery Tools  Force writing  Checkpoint time  End of transaction  Database backup  Complete  Incremental

39 Slide 39 Chapter 15: Transaction Management Recovery from a Media Failure  Restore database from the most recent backup  Redo all committed transactions since the most recent backup  Restart active transactions

40 Slide 40 Chapter 15: Transaction Management Recovery Timeline

41 Slide 41 Chapter 15: Transaction Management 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

42 Slide 42 Chapter 15: Transaction Management Immediate Update Recovery

43 Slide 43 Chapter 15: Transaction Management Deferred Update Recovery

44 Slide 44 Chapter 15: Transaction Management 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

45 Slide 45 Chapter 15: Transaction Management 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

46 Slide 46 Chapter 15: Transaction Management 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.

47 Slide 47 Chapter 15: Transaction Management 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

48 Slide 48 Chapter 15: Transaction Management 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

49 Slide 49 Chapter 15: Transaction Management Transaction Design Issues  Transaction boundary  Isolation levels  Deferred constraint checking  Savepoints

50 Slide 50 Chapter 15: Transaction Management 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

51 Slide 51 Chapter 15: Transaction Management Registration Form Example

52 Slide 52 Chapter 15: Transaction Management 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

53 Slide 53 Chapter 15: Transaction Management 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

54 Slide 54 Chapter 15: Transaction Management 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

55 Slide 55 Chapter 15: Transaction Management 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

56 Slide 56 Chapter 15: Transaction Management Tennis Reservation Calendar

57 Slide 57 Chapter 15: Transaction Management Tennis Reservation Selection

58 Slide 58 Chapter 15: Transaction Management Choose Partner(s)

59 Slide 59 Chapter 15: Transaction Management 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

60 Slide 60 Chapter 15: Transaction Management 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

61 Slide 61 Chapter 15: Transaction Management SQL Isolation Levels LevelXLocksSLocksPLocksInterference Read uncommitted None Uncommitted dependency Read committed LongShortNoneAll except uncommitted dependency Repeatable read Long Short (S), Long (X) Phantom reads SerializableLong None

62 Slide 62 Chapter 15: Transaction Management Scholar’s Lost Update Transaction ATimeTransaction B Obtain S lock on SRT1T1 Read SR (10)T2T2 Release S lock on SRT3T3 If SR > 0 then SR = SR -1T4T4 T5T5 Obtain S lock on SR T6T6 Read SR (10) T7T7 Release S lock on SR T8T8 If SR > 0 then SR = SR -1 Obtain X lock on SRT9T9 Write SR (9)T 10 CommitT 11 T 12 Obtain X lock on SR T 13 Write SR (9)

63 Slide 63 Chapter 15: Transaction Management 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

64 Slide 64 Chapter 15: Transaction Management Save Points  Some transactions have tentative actions  SAVEPOINT statement determines intermediate points  ROLLBACK to specified save points

65 Slide 65 Chapter 15: Transaction Management Workflow Management  Workflow description  Enabling technologies  Advanced transaction management

66 Slide 66 Chapter 15: Transaction Management 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

67 Slide 67 Chapter 15: Transaction Management Workflow Classification

68 Slide 68 Chapter 15: Transaction Management Enabling Technologies  Distributed object management  Many kinds of non traditional data  Data often dispersed in location  Workflow modeling  Specification  Simulation  Optimization

69 Slide 69 Chapter 15: Transaction Management Advanced Transaction Management  Conversational transactions  Transactions with complex structure  Transactions involving legacy systems  Compensating transactions  More flexible transaction processing

70 Slide 70 Chapter 15: Transaction Management 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


Download ppt "Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael V. Mannino. All rights reserved. Chapter 15 Transaction."

Similar presentations


Ads by Google