Chapter 15Chapter 15Transaction Management
Outline Outline
Transaction basicsConcurrency controlRecovery managementTransaction design issuesWorkflow management
Transaction DefinitionTransaction DefinitionSupports daily operations of an
organizationCollection of database operationsReliably and efficiently processed as one
unit of workNo lost data
– Interference among multiple users– Failures
Airline Transaction ExampleAirline Transaction ExampleSTART TRANSACTION
Display 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
Transaction PropertiesTransaction PropertiesAtomic: 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
Transaction Processing ServicesTransaction Processing ServicesConcurrency controlRecovery managementService characteristics
– Transparent– Consume significant resources– Significant cost component
Concurrency ControlConcurrency ControlProblem definitionConcurrency control problemsConcurrency control tools
Concurrency Control ProblemConcurrency Control ProblemObjective:
– 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
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)
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
Inconsistent Retrieval ProblemsInconsistent Retrieval ProblemsInterference causes inconsistency among
multiple retrievals of a subset of dataIncorrect summary Phantom readNon repeatable read
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
Locking FundamentalsLocking FundamentalsFundamental 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
Locking GranularityLocking GranularityDatabase
Table
Page
Row
Column
Index
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)
Deadlock ResolutionDeadlock ResolutionDetection
– 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
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
2PL Implementation2PL Implementation
Time
Growing phase
BOT EOT
Shrinkingphase
Lock
s he
ld
Optimistic ApproachesOptimistic ApproachesAssumes conflicts are rareNo locksCheck for conflicts
– After each read and write– At end of transaction
Evaluation– Less overhead– More variability
Recovery ManagementRecovery ManagementDevice characteristics and failure typesRecovery toolsRecovery processes
Storage Device BasicsStorage Device BasicsVolatile: 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
Failure TypesFailure TypesLocal
– 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
Transaction LogTransaction LogHistory of database changesLarge storage overheadOperations
– Undo: revert to previous state– Redo: reestablish a new state
Fundamental tool of recovery management
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
CheckpointsCheckpointsReduces 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
Other Recovery ToolsOther Recovery ToolsForce writing
– Checkpoint time– End of transaction
Database backup– Complete– Incremental
Recovery from a Media FailureRecovery from a Media FailureRestore database from the most recent
backupRedo all committed transactions since the
most recent backupRestart active transactions
Recovery TimelineRecovery Timeline
Time
T1
T5
T4
T3
T2
Checkpoint Failure
Recovery ProcessesRecovery ProcessesDepend 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
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
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
Transaction Design IssuesTransaction Design IssuesTransaction boundaryIsolation levelsDeferred constraint checkingSavepoints
Transaction Boundary Transaction Boundary DecisionsDecisionsDivision of work into transactionsObjective: minimize transaction durationConstraint: enforcement of important
integrity constraintsTransaction boundary decision can affect
hot spots
Registration Form ExampleRegistration Form Example
Transaction Boundary ChoicesTransaction Boundary ChoicesOne 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
Isolation LevelsIsolation LevelsDegree to which a transaction is separated
from the actions of other transactionsBalance concurrency control overhead
with interference problemsSome transactions can tolerate
uncommitted dependency and inconsistent retrieval problems
Specify using the SET TRANSACTION statement
SQL Isolation LevelsSQL Isolation LevelsLevel 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
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
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
T12 Obtain X lock on SR
T13 Write SR (9)
Integrity Constraint TimingIntegrity Constraint TimingMost constraints checked immediatelyCan defer constraint checking to EOTSQL SET CONSTRAINTS statement
Save PointsSave PointsSome transactions have tentative actionsSAVEPOINT statement determines
intermediate pointsROLLBACK to specified save points
Workflow ManagementWorkflow ManagementWorkflow descriptionEnabling technologiesAdvanced transaction management
WorkflowsWorkflowsSet of tasks to accomplish a business
processHuman-oriented vs. computer-oriented
– Amount of judgment– Amount of automation
Task structure vs. task complexity– Relationships among tasks– Difficulty of performing individual tasks
Enabling TechnologiesEnabling TechnologiesDistributed object management
– Many kinds of non traditional data– Data often dispersed in location
Workflow modeling– Specification– Simulation– Optimization
Advanced Transaction Advanced Transaction ManagementManagementConversational transactionsTransactions with complex structureTransactions involving legacy systemsCompensating transactionsMore flexible transaction processing
SummarySummaryTransaction: 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