Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All...

45
Chapter 15 Chapter 15 Transaction Management
  • 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...

Page 1: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

Chapter 15Chapter 15Transaction Management

Page 2: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Outline Outline

Transaction basicsConcurrency controlRecovery managementTransaction design issuesWorkflow management

Page 3: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 4: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 5: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 6: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 7: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Concurrency ControlConcurrency Control

Problem definitionConcurrency control problemsConcurrency control tools

Page 8: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 9: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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)

Page 10: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 11: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 12: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 13: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 14: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Locking GranularityLocking GranularityDatabase

Table

Page

Row

Column

Index

Page 15: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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)

Page 16: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 17: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 18: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 19: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 20: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Recovery ManagementRecovery Management

Device characteristics and failure typesRecovery toolsRecovery processes

Page 21: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 22: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 23: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 24: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 25: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 26: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 27: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 28: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Recovery TimelineRecovery Timeline

Time

T1

T5

T4

T3

T2

Checkpoint Failure

Page 29: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 30: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 31: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 32: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Transaction Design IssuesTransaction Design Issues

Transaction boundaryIsolation levelsDeferred constraint checkingSavepoints

Page 33: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 34: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Registration Form ExampleRegistration Form Example

Page 35: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 36: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 37: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 38: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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)

Page 39: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 40: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 41: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Workflow ManagementWorkflow Management

Workflow descriptionEnabling technologiesAdvanced transaction management

Page 42: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 43: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 44: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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

Page 45: Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.

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