450 likes | 640 Views
Transaction Management. by Benjamin Nguyen. Overview. Transaction Properties of transaction Pessimistic & optimistic techniques Locking No lock techniques Oracle recovery tools. Transaction.
E N D
Transaction Management by Benjamin Nguyen
Overview • Transaction • Properties of transaction • Pessimistic & optimistic techniques • Locking • No lock techniques • Oracle recovery tools
Transaction • An action, or series of actions, carried out by a single user or application program, which reads or updates the contents of the database
Properties of Transaction • ACID • Atomicity: ‘all or nothing’ property. (Responsible by DBMS recovery subsystem) • Consistency: database must be transform from one consistent state to another consistent state.
Properties of Transaction (cont.) • ACID • Isolation: transaction execute independently of one another. (Responsible by concurrency control) • Durability: The effects of a successfully completed (committed) transaction are permanently recorded in the database and must not be lost because of a subsequent failure (Responsible by recovery subsystem)
Pessimistic & Optimistic techniques • Pessimistic • Check for conflict when read/write • Optimistic • Only check for conflict when the transaction wishes to commit
Pessimistic & Optimistic techniques • Pessimistic • Using locking techniques • Optimistic • The concurrency control that does not use lock and it involve the following phases: • Read phase • Validation phase • Write phase
Optimistic Techniques Read phase Write phase write Read Validation commit
Read phase • Extends from start of transaction to just before commit • Read from database • Store in local variable
Validation phase • Follow the read phase • Purpose is to ensure that data is still current.
Write phase • Follow after validation • Updated variable from local variable can now be applied to database
Pessimistic techniques • Using locking technique to reduce conflict and ensure data consistency. • The lost update problem
The lost update problem T1 T2 balx = $0 balx = $100
The lost update problem T1 T2 balx = $100 balx = $200
The lost update problem T1 T2 balx = $90 balx = $200
The lost update problem T1 T2 balx = $90 balx = $200 bal should be = (100+100 -10) = $190 Since T1 committed after T2, it written over the value of T2
The uncommitted dependency (or dirty read) problem T3 Balx should be $90
How to deal with these problem? • Serial schedule • A schedule where the operations of each transaction are executed consecutively without any interleaved operations from the other transaction • Locking technique • A procedure used to control concurrent access to data. When one transaction is accessing the database, a lock may deny access to other transactions to prevent incorrect results.
Serializability Not serial Serial
Serializability (cont.) • The objective of serializability is to find nonserial schedules that allow transactions to execute concurrently without interfering with one another • Ex: • two transaction that read data only • Two transaction that read or update separate data item
Serialiability (cont.) • 2 types of serializability: • Conflict serializability • Non-conflict serializable
Rules for constructing precedence (or serialization) graph • Create a node for each transaction Create a directed edge Ti->Tj if • Tj reads the value of an item written by Ti • Tj writes a value into an item after it has been read by Ti • Tj writes a value into an item after it has been written by Tj
Precedence graph Step 1 T9 T10
Precedence graph Rule 2 Create a directed edge Ti->Tj, if Tj reads the value of an item written by Ti T9 T10
Precedence graph Rule 2 Create a directed edge Ti->Tj, if Tj reads the value of an item written by Ti T9 T10
Precedence graph (cont.) Rule2 T7 T8
Precedence graph (cont.) Rule2 T7 T8
Lock • 2 types of Lock: • Shared lock (read only lock) • If a transaction has a shared lock on a data item, it can read the item but not update it. • Exclusive lock (read/update lock) • If a transaction has an exclusive lock on a data item, it can both read and update the item
Deadlock • An impasse that may result when two (or more) transactions are each waiting for locks to be released that are held by the other
How to handle deadlock • 3 methods • Timeouts • Deadlock prevention (not very popular because it is too complicated) • Wait-die • Wound-wait • Deadlock detection (using wait-for graph)
Rules to construct wait-for graph • Create a node for each transaction • Create a directed edge Ti->Tj, if transaction Ti is waiting to lock an item that is currently locked by Tj
Wait-for graph (WFG) X Y lock T17 T18
Wait-for graph (WFG) X Y lock lock T17 T18
Wait-for graph (WFG) X Y lock lock T17 T18
Wait-for graph (WFG) X Y lock lock T17 T18
Recovery • Generally there are four different types of media that can be use for storage • Main memory • Magnetic disk • Magnetic tape • Optical disk
Types of media Optical disk Magnetic tape Magnetic disk Main memory Degree of reliability
Recovery (cont.) • Oracle provides various tools for system recovery • Recovery manager • Provide you with way to create back up of your data • Help you restore you data incase of failure • Instance recover • After a crash, Oracle use the information in the control file to recover the database to the consistent state before the crash
Recovery (cont.) • Standby database • Allow a standby database to be maintained in the event of the primary database failing