Chapter 5
Chapter 5
Transaction Management
Chapter Outline
1 Introduction to Transaction Processing 2 Transaction and System Concepts 3 Desirable Properties of Transactions 4 Characterizing Schedules based on Recoverability 5 Characterizing Schedules based on Serializability 6 Databases Concurrency Control
1.Purpose of Concurrency Control 2.Two-Phase locking
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 2
Chapter Outline
Databases Recovery
1. Purpose of Database Recovery 2. Types of Failure 3 . Transaction Log 4 . Data Updates 5. Data Caching 6 . Transaction Roll-back (Undo) and Roll-Forward 7 . Checkpointing 8 . Recovery schemes
Slide 5- 3
Single-User System:
Multiuser System:
Concurrency
Concurrent execution of processes is interleaved in a single CPU Processes are concurrently executed in multiple CPUs.
Parallel processing:
Slide 5- 4
A Transaction:
Logical unit of database processing that includes one or more access operations (read -retrieval, write - insert or update, delete).
A transaction (set of operations) may be stand-alone specified in a high level language like SQL submitted interactively, or may be embedded within a program. Transaction boundaries:
An application program may contain several transactions separated by the Begin and End transaction boundaries.
Slide 5- 5
Slide 5- 6
Find the address of the disk block that contains item X. Copy that disk block into a buffer in main memory (if that disk block is not already in some main memory buffer). Copy item X from the buffer to the program variable named X.
Slide 5- 7
Find the address of the disk block that contains item X. Copy that disk block into a buffer in main memory (if that disk block is not already in some main memory buffer). Copy item X from the program variable named X into its correct location in the buffer. Store the updated block from the buffer back to disk (either immediately or at some later point in time).
Slide 5- 8
Slide 5- 9
This occurs when two transactions that access the same database items have their operations interleaved in a way that makes the value of some database item incorrect. This occurs when one transaction updates a database item and then the transaction fails for some reason (see Section 17.1.4). The updated item is accessed by another transaction before it is changed back to its original value. If one transaction is calculating an aggregate summary function on a number of records while other transactions are updating some of these records, the aggregate function may calculate some values before they are updated and others after they are updated.
Slide 5- 10
Slide 5- 11
Slide 5- 12
Slide 5- 13
Slide 5- 14
Slide 5- 15
Slide 5- 16
A transaction is an atomic unit of work that is either completed in its entirety or not done at all. For recovery purposes, the system needs to keep track of when the transaction starts, terminates, and commits or aborts. Transaction states: Active state Partially committed state Committed state Failed state Terminated State
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 17
begin_transaction: This marks the beginning of transaction execution. read or write: These specify read or write operations on the database items that are executed as part of a transaction. end_transaction: This specifies that read and write transaction operations have ended and marks the end limit of transaction execution. At this point it may be necessary to check whether the changes introduced by the transaction can be permanently applied to the database or whether the transaction has to be aborted because it violates concurrency control or for some other reason.
Slide 5- 18
commit_transaction: This signals a successful end of the transaction so that any changes (updates) executed by the transaction can be safely committed to the database and will not be undone. rollback (or abort): This signals that the transaction has ended unsuccessfully, so that any changes or effects that the transaction may have applied to the database must be undone.
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 19
undo: Similar to rollback except that it applies to a single operation rather than to a whole transaction. redo: This specifies that certain transaction operations must be redone to ensure that all the operations of a committed transaction have been applied successfully to the database.
Slide 5- 20
Slide 5- 21
Log or Journal: The log keeps track of all transaction operations that affect the values of database items.
This information may be needed to permit recovery from transaction failures. The log is kept on disk, so it is not affected by any type of failure except for disk or catastrophic failure. In addition, the log is periodically backed up to archival storage (tape) to guard against such catastrophic failures.
Slide 5- 22
T in the following discussion refers to a unique transaction-id that is generated automatically by the system and is used to identify each transaction: Types of log record: [start_transaction,T]: Records that transaction T has started execution. [write_item,T,X,old_value,new_value]: Records that transaction T has changed the value of database item X from old_value to new_value. [read_item,T,X]: Records that transaction T has read the value of database item X. [commit,T]: Records that transaction T has completed successfully, and affirms that its effect can be committed (recorded permanently) to the database. [abort,T]: Records that transaction T has been aborted.
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 23
Protocols for recovery that avoid cascading rollbacks do not require that read operations be written to the system log, whereas other protocols require these entries for recovery. Strict protocols require simpler write entries that do not include new_value (see Section 17.4).
Slide 5- 24
2.
Slide 5- 25
A transaction T reaches its commit point when all its operations that access the database have been executed successfully and the effect of all the transaction operations on the database has been recorded in the log. Beyond the commit point, the transaction is said to be committed, and its effect is assumed to be permanently recorded in the database. The transaction then writes an entry [commit,T] into the log.
Needed for transactions that have a [start_transaction,T] entry into the log but no commit entry [commit,T] into the log.
Slide 5- 26
Slide 5- 27
Slide 5- 28
When transactions are executing concurrently in an interleaved fashion, the order of execution of operations from the various transactions forms what is known as a transaction schedule (or history).
It is an ordering of the operations of the transactions subject to the constraint that, for each transaction Ti that participates in S, the operations of T1 in S must appear in the same order in which they occur in T1. Note, however, that operations from other transactions Tj can be interleaved with the operations of Ti in S.
Slide 5- 29
Slide 5- 30
A schedule in which uncommitted transactions that read an item from a failed transaction must be rolled back.
A schedule in which a transaction can neither read or write an item X until the last transaction that wrote X has committed.
Strict Schedules:
Slide 5- 31
Serial schedule:
A schedule S is serial if, for every transaction T participating in the schedule, all the operations of T are executed consecutively in the schedule.
Serializable schedule:
Slide 5- 32
Being serializable is not the same as being serial Being serializable implies that the schedule is a correct schedule.
It will leave the database in a consistent state. The interleaving is appropriate and will result in a state as if the transactions were serially executed, yet will achieve efficiency due to concurrent execution.
Slide 5- 33
Looks at only read_Item (X) and write_Item (X) operations Constructs a precedence graph (serialization graph) - a graph with directed edges An edge is created from Ti to Tj if one of the operations in Ti appears before a conflicting operation in Tj The schedule is serializable if and only if the precedence graph has no cycles.
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 34
FIGURE 17.7 Constructing the precedence graphs for schedules A and D from Figure 17.5 to test for conflict serializability.
(a) Precedence graph for serial schedule A. (b) Precedence graph for serial schedule B. (c) Precedence graph for schedule C (not serializable). (d) Precedence graph for schedule D (serializable, equivalent to schedule A).
Slide 5- 35
Slide 5- 36
Slide 5- 37
Slide 5- 38
To enforce Isolation (through mutual exclusion) among conflicting transactions. To preserve database consistency through consistency preserving execution of transactions. To resolve read-write and write-write conflicts.
Example:
In concurrent execution environment if T1 conflicts with T2 over a data item A, then the existing concurrency control decides if T1 or T2 should get the A and if the other transaction is rolled-back or waits.
Slide 5- 39
(a) permission to Read (b) permission to Write a data item for a transaction. Lock (X). Data item X is locked in behalf of the requesting transaction.
Example:
Unlocking is an operation which removes these permissions from the data item. Example:
Slide 5- 40
More than one transaction can apply share lock on X for reading its value but no write lock can be applied on X by any other transaction. Only one write lock on X can exist at any time and no shared lock can be applied by any other transaction on X.
Read
Read
Conflict matrix
Write N N
Slide 5- 41
Y N
Write
Lock Manager:
Managing locks on data items. Lock manager uses it to store the identify of transaction locking a data item, the data item, lock mode and pointer to the next data item locked. One simple way to implement a lock table is through linked list.
Lock table:
Transaction ID Data item id lock mode Ptr to next data item T1 X1 Read Next
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 42
Database requires that all transactions should be well-formed. A transaction is well-formed if:
It must lock the data item before it reads or writes to it. It must not lock an already locked data items and it must not try to unlock a free data item.
Slide 5- 43
B: if LOCK (X) = 0 (*item is unlocked*) then LOCK (X) 1 (*lock the item*) else begin wait (until lock (X) = 0) and the lock manager wakes up the transaction); goto B end;
Slide 5- 44
LOCK (X) 0 (*unlock the item*) if any transactions are waiting then wake up one of the waiting the transactions;
Slide 5- 45
begin LOCK (X) read-locked; no_of_reads (X) 1; end else if LOCK (X) read-locked then no_of_reads (X) no_of_reads (X) +1 else begin wait (until LOCK (X) = unlocked and the lock manager wakes up the transaction); go to B end;
Slide 5- 46
begin LOCK (X) read-locked; no_of_reads (X) 1; end else if LOCK (X) read-locked then no_of_reads (X) no_of_reads (X) +1 else begin wait (until LOCK (X) = unlocked and the lock manager wakes up the transaction); go to B end;
Slide 5- 47
begin LOCK (X) unlocked; wakes up one of the transactions, if any end else if LOCK (X) read-locked then begin no_of_reads (X) no_of_reads (X) -1 if no_of_reads (X) = 0 then begin LOCK (X) = unlocked; wake up one of the transactions, if any end end;
Slide 5- 48
Slide 5- 49
Slide 5- 50
Slide 5- 51
T2
Result
X=50; Y=50 Nonserializable because it. violated two-phase policy.
Time
read_lock (X); read_item (X); unlock (X); write_lock (Y); read_item (Y); Y:=X+Y; write_item (Y); unlock (Y);
Slide 5- 52
Slide 5- 53
Slide 5- 54
Deadlock
T2 T1 and T2 did follow two-phase policy but they are deadlock
Slide 5- 55
A transaction locks all data items it refers to before it begins execution. This way of locking prevents deadlock since a transaction never waits for a data item. The conservative two-phase locking uses this approach.
Slide 5- 56
In this approach, deadlocks are allowed to happen. The scheduler maintains a wait-for-graph for detecting cycle. If a cycle exists, then one transaction involved in the cycle is selected (victim) and rolled-back. A wait-for-graph is created using the lock table. As soon as a transaction is blocked, it is added to the graph. When a chain like: Ti waits for Tj waits for Tk waits for Ti or Tj occurs, then this creates a cycle. One of the transaction o
Slide 5- 57
There are many variations of two-phase locking algorithm. Some avoid deadlock by not letting the cycle to complete. That is as soon as the algorithm discovers that blocking a transaction is likely to create a cycle, it rolls back the transaction. Wound-Wait and Wait-Die algorithms use timestamps to avoid deadlocks by rolling-back victim.
Slide 5- 58
Starvation occurs when a particular transaction consistently waits or restarted and never gets a chance to proceed further. In a deadlock resolution it is possible that the same transaction may consistently be selected as victim and rolled-back. This limitation is inherent in all priority based scheduling mechanisms. In Wound-Wait scheme a younger transaction may always be wounded (aborted) by a long running older transaction which may create starvation.
Slide 5- 59
A monotonically increasing variable (integer) indicating the age of an operation or a transaction. A larger timestamp value indicates a more recent event or operation. Timestamp based algorithm uses timestamp to serialize the execution of concurrent transactions.
Slide 5- 60
Database Recovery
1 Purpose of Database Recovery To bring the database into the last consistent state, which existed prior to the failure. To preserve transaction properties (Atomicity, Consistency, Isolation and Durability). Example: If the system crashes before a fund transfer transaction completes its execution, then either one or both accounts may have incorrect value. Thus, the database must be restored to the state before the transaction modified any of the accounts.
Slide 5- 61
Database Recovery
2 Types of Failure
Transaction failure: Transactions may fail because of incorrect input, deadlock, incorrect synchronization. System failure: System may fail because of addressing error, application error, operating system fault, RAM failure, etc. Media failure: Disk head crash, power disruption, etc.
Slide 5- 62
Database Recovery
3 Transaction Log
For recovery from any type of failure data values prior to modification (BFIM - BeFore Image) and the new value after modification (AFIM AFter Image) are required. These values and other information is stored in a sequential file called Transaction log. A sample log is given below. Back P and Next P point to the previous and next log records of the same transaction.
T ID Back P Next P Operation Data item Begin T1 0 1 T1 1 4 Write X Begin T2 0 8 T1 2 5 W Y T1 4 7 R M T3 0 9 R N T1 5 nil End
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
BFIM X = 100
AFIM X = 200
Database Recovery
4 Data Update
Immediate Update: As soon as a data item is modified in cache, the disk copy is updated. Deferred Update: All modified data items in the cache is written either after a transaction ends its execution or after a fixed number of transactions have completed their execution. Shadow update: The modified version of a data item does not overwrite its disk copy but is written at a separate disk location. In-place update: The disk version of the data item is overwritten by the cache version.
Slide 5- 64
Database Recovery
5 Data Caching
Data items to be modified are first stored into database cache by the Cache Manager (CM) and after modification they are flushed (written) to the disk. The flushing is controlled by Modified and PinUnpin bits.
Pin-Unpin: Instructs the operating system not to flush the data item. Modified: Indicates the AFIM of the data item.
Slide 5- 65
Database Recovery
6 Transaction Roll-back (Undo) and RollForward (Redo)
Undo: Restore all BFIMs on to disk (Remove all AFIMs). Redo: Restore all AFIMs on to disk.
Database recovery is achieved either by performing only Undos or only Redos or by a combination of the two. These operations are recorded in the log as they happen.
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 66
Database Recovery
Slide 5- 67
Database Recovery
Slide 5- 68
Database Recovery
Roll-back: One execution of T1, T2 and T3 as recorded in the log.
Slide 5- 69
Database Recovery
Write-Ahead Logging When in-place update (immediate or deferred) is used then log is necessary for recovery and it must be available to recovery manager. This is achieved by Write-Ahead Logging (WAL) protocol. WAL states that
For Undo: Before a data items AFIM is flushed to the database disk (overwriting the BFIM) its BFIM must be written to the log and the log must be saved on a stable store (log disk). For Redo: Before a transaction executes its commit operation, all its AFIMs must be written to the log and the log must be saved on a stable store.
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 70
Database Recovery
7 Checkpointing
Time to time (randomly or under some criteria) the database flushes its buffer to database disk to minimize the task of recovery. The following steps defines a checkpoint operation:
1. 2. 3. 4. Suspend execution of transactions temporarily. Force write modified buffer data to disk. Write a [checkpoint] record to the log, save the log to disk. Resume normal transaction execution.
During recovery redo or undo is required to transactions appearing after [checkpoint] record.
Slide 5- 71
Database Recovery
Steal/No-Steal and Force/No-Force
Slide 5- 72
Database Recovery
8 Recovery Scheme Deferred Update (No Undo/Redo)
The data update goes as follows: A set of transactions records their updates in the log. At commit point under WAL scheme these updates are saved on database disk. After reboot from a failure the log is used to redo all the transactions affected by this failure. No undo is required because no AFIM is flushed to the disk before a transaction commits.
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 73
Database Recovery
Deferred Update in a single-user system There is no concurrent data sharing in a single user system. The data update goes as follows:
A set of transactions records their updates in the log. At commit point under WAL scheme these updates are saved on database disk.
After reboot from a failure the log is used to redo all the transactions affected by this failure. No undo is required because no AFIM is flushed to the disk before a transaction commits.
Slide 5- 74
Database Recovery
Slide 5- 75
Database Recovery
Deferred Update with concurrent users This environment requires some concurrency control mechanism to guarantee isolation property of transactions. In a system recovery transactions which were recorded in the log after the last checkpoint were redone. The recovery manager may scan some of the transactions recorded before the checkpoint to get the AFIMs.
Slide 5- 76
Database Recovery
Slide 5- 77
Database Recovery
Deferred Update with concurrent users Two tables are required for implementing this protocol:
Active table: All active transactions are entered in this table. Commit table: Transactions to be committed are entered in this table.
During recovery, all transactions of the commit table are redone and all transactions of active tables are ignored since none of their AFIMs reached the database. It is possible that a commit table transaction may be redone twice but this does not create any inconsistency because of a redone is idempotent, that is, one redone for an AFIM is equivalent to multiple redone for the same AFIM.
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 78
Database Recovery
Recovery Techniques Based on Immediate Update Undo/No-redo Algorithm
In this algorithm AFIMs of a transaction are flushed to the database disk under WAL before it commits. For this reason the recovery manager undoes all transactions during recovery. No transaction is redone. It is possible that a transaction might have completed execution and ready to commit but this transaction is also undone.
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 79
Database Recovery
Recovery Techniques Based on Immediate Update Undo/Redo Algorithm (Single-user environment)
Recovery schemes of this category apply undo and also redo for recovery. In a single-user environment no concurrency control is required but a log is maintained under WAL. Note that at any time there will be one transaction in the system and it will be either in the commit table or in the active table. The recovery manager performs:
Undo of a transaction if it is in the active table. Redo of a transaction if it is in the commit table.
Slide 5- 80
Database Recovery
Recovery Techniques Based on Immediate Update Undo/Redo Algorithm (Concurrent execution) Recovery schemes of this category applies undo and also redo to recover the database from failure. In concurrent execution environment a concurrency control is required and log is maintained under WAL. Commit table records transactions to be committed and active table records active transactions. To minimize the work of the recovery manager checkpointing is used. The recovery performs:
Undo of a transaction if it is in the active table. Redo of a transaction if it is in the commit table.
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 81
Database Recovery
Shadow Paging The AFIM does not overwrite its BFIM but recorded at another place on the disk. Thus, at any time a data item has AFIM and BFIM (Shadow copy of the data item) at two different places on the disk.
Y X' Database
Y'
X and Y: Shadow copies of data items X' and Y': Current copies of data items
Copyright 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 82
Database Recovery
Shadow Paging To manage access of data items by concurrent transactions two directories (current and shadow) are used.
Slide 5- 83