Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All...
-
date post
20-Dec-2015 -
Category
Documents
-
view
222 -
download
0
Transcript of Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All...
Chapter 15Chapter 15Transaction Management
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Outline Outline
Transaction basicsConcurrency controlRecovery managementTransaction design issuesWorkflow management
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Transaction DefinitionTransaction Definition
Supports daily operations of an organization
Collection of database operationsReliably and efficiently processed as one
unit of workNo lost data
– Interference among multiple users– Failures
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Airline Transaction ExampleAirline Transaction Example
START TRANSACTIONDisplay greetingGet reservation preferences from userSELECT departure and return flight recordsIf reservation is acceptable then
UPDATE seats remaining of departure flight recordUPDATE seats remaining of return flight recordINSERT reservation recordPrint ticket if requested
End IfOn Error: ROLLBACK
COMMIT
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Transaction PropertiesTransaction Properties
Atomic: all or nothingConsistent: database must be consistent
before and after a transactionIsolated: no unwanted interference from
other usersDurable: database changes are permanent
after the transaction completes
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Transaction Processing ServicesTransaction Processing Services
Concurrency controlRecovery managementService characteristics
– Transparent– Consume significant resources– Significant cost component
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Concurrency ControlConcurrency Control
Problem definitionConcurrency control problemsConcurrency control tools
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Concurrency Control ProblemConcurrency Control Problem
Objective: – Maximize work performed– Throughput: number of transactions processed
per unit timeConstraint:
– 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 ProblemLost Update Problem
Transaction A Time Transaction B
Read SR (10) T1 T2 Read SR (10) If SR > 0 then SR = SR -1
T3
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 Uncommitted Dependency ProblemProblem
Transaction A Time Transaction B
Read SR (10) T1 SR = SR - 1 T2 Write SR (9) T3 T4 Read SR (9) ROLLBACK T5
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Inconsistent Retrieval ProblemsInconsistent Retrieval Problems
Interference causes inconsistency among multiple retrievals of a subset of data
Incorrect summary Phantom readNon repeatable read
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Incorrect Summary ProblemIncorrect Summary ProblemTransaction A Time Transaction B Read SR1 (10) T1 SR1 = SR1 - 1 T2 Write SR1 (9) T3 T4 Read SR1 (9) T5 Sum = Sum + SR1 T6 Read SR2 (5) T7 Sum = Sum + SR2 Read SR2 (5) T8 SR2 = SR2 - 1 T9 Write SR2 (4) T10
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Locking FundamentalsLocking Fundamentals
Fundamental tool of concurrency controlObtain lock before accessing an itemWait if a conflicting lock is held
– Shared lock: conflicts with exclusive locks– Exclusive lock: conflicts with all other kinds
of locksConcurrency control manager maintains
the lock table
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Locking GranularityLocking GranularityDatabase
Table
Page
Row
Column
Index
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Deadlock (Mutual Waiting)Deadlock (Mutual Waiting)
Transaction A Time Transaction B
XLock SR1 T1
T2 XLock SR2
XLock SR2 (wait) T3
T4 XLock SR1 (wait)
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Deadlock ResolutionDeadlock 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)Two Phase Locking (2PL)
Protocol to prevent lost update problemsAll transactions must followConditions
– 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.
2PL Implementation2PL Implementation
Time
Growing phase
BOT EOT
Shrinkingphase
Lo
cks
he
ld
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Optimistic ApproachesOptimistic Approaches
Assumes conflicts are rareNo locksCheck 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 ManagementRecovery Management
Device characteristics and failure typesRecovery toolsRecovery processes
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Storage Device BasicsStorage Device Basics
Volatile: loses state after a shutdownNonvolatile: retains state after a shutdownNonvolatile is more reliable than volatile
but failures can cause loss of dataUse multiple levels and redundant levels
of nonvolatile storage for valuable data
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Failure TypesFailure 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 LogTransaction Log
History of database changesLarge storage overheadOperations
– 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 ExampleTransaction Log Example
LSN TransNo Action Time Table Row Column Old New
1 101001 START 10:29 2 101001 UPDATE 10:30 Acct 10001 AcctBal 100 200 3 101001 UPDATE 10:30 Acct 15147 AcctBal 500 400 4 101001 INSERT 10:32 Hist 25045 * <1002,
500, …>
5 101001 COMMIT 10:33
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
CheckpointsCheckpoints
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 ToolsOther 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 FailureRecovery 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 TimelineRecovery Timeline
Time
T1
T5
T4
T3
T2
Checkpoint Failure
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Recovery ProcessesRecovery Processes
Depend on timing of database writesImmediate 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 RecoveryImmediate Update RecoveryClass Description Restart Work
T1 Finished before CP None
T2 Started before CP; finished before failure
Redo forward from checkpoint
T3 Started after CP; finished before failure
Redo forward from checkpoint
T4 Started before CP; not yet finished
Undo backwards from most recent log record
T5 Started after CP; not yet finished
Undo backwards from most recent log record
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Deferred Update RecoveryDeferred Update RecoveryClass Description Restart Work
T1 Finished before CP None
T2 Started before CP; finished before failure
Redo forward from first log record
T3 Started after CP; finished before failure
Redo forward from first log record
T4 Started before CP; not yet finished
None
T5 Started after CP; not yet finished
None
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Transaction Design IssuesTransaction Design Issues
Transaction boundaryIsolation levelsDeferred constraint checkingSavepoints
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Transaction Boundary DecisionsTransaction Boundary Decisions
Division of work into transactionsObjective: minimize transaction durationConstraint: enforcement of important
integrity constraintsTransaction boundary decision can affect
hot spots
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Registration Form ExampleRegistration Form Example
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Transaction Boundary ChoicesTransaction Boundary Choices
One transaction for the entire formOne transaction for the main form and one
transaction for all subform recordsOne 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 LevelsIsolation 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 LevelsSQL 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
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Scholar’s Lost UpdateScholar’s Lost UpdateTransaction A Time Transaction BObtain S lock on SR T1
Read SR (10) T2
Release S lock on SR T3
If SR > 0 then SR = SR -1 T4
T5Obtain S lock on SR
T6Read SR (10)
T7Release S lock on SR
T8If SR > 0 then SR = SR -1
Obtain X lock on SR T9
Write SR (9) T10
Commit T11
T12Obtain X lock on SR
T13Write SR (9)
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Integrity Constraint TimingIntegrity Constraint Timing
Most constraints checked immediatelyCan defer constraint checking to EOTSQL SET CONSTRAINTS statement
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Save PointsSave Points
Some transactions have tentative actionsSAVEPOINT statement determines
intermediate pointsROLLBACK to specified save points
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Workflow ManagementWorkflow Management
Workflow descriptionEnabling technologiesAdvanced transaction management
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
WorkflowsWorkflows
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 TechnologiesEnabling 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 Advanced Transaction ManagementManagementConversational transactionsTransactions with complex structureTransactions involving legacy systemsCompensating transactionsMore flexible transaction processing
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
SummarySummary
Transaction: user-defined collection of workDBMSs support ACID propertiesKnowledge of concurrency control and
recovery important for managing databasesTransaction design issues are importantTransaction processing is an important part
of workflow management