Transactions For Class Good
Transactions For Class Good
Transactions For Class Good
What is a transaction
A transaction is the basic logical unit of execution in an information system. A transaction is a sequence of operations that must be executed as a whole, taking a consistent (& correct) database state into another consistent (& correct) database state; A collection of actions that make consistent transformations of system states while preserving system consistency An indivisible unit of processing
database in a consistent state database in a consistent state
Transfer 500
end Transaction
Recovery
System failures, either hardware or software, must not result in an inconsistent database
The database is restored to some state from the past so that a correct stateclose to the time of failurecan be reconstructed from the past state. A DBMS ensures that if a transaction executes some updates and then a failure occurs before the transaction reaches normal termination, then those updates are undone. The statements COMMIT and ROLLBACK (or their equivalent) ensure Transaction Atomicity
Recovery
Mirroring
keep two copies of the database and maintain them simultaneously
Backup
periodically dump the complete state of the database to some form of tertiary storage
System Logging
the log keeps track of all transaction operations affecting the values of database items. The log is kept on disk so that it is not affected by failures except for disk and catastrophic failures.
Non-catastrophic failure
Reverse the changes that caused the inconsistency by undoing the operations and possibly redoing legitimate changes which were lost The entries kept in the system log are consulted during recovery. No need to use the complete archival copy of the database.
Transaction States
For recovery purposes the system needs to keep track of when a transaction starts, terminates and commits. Begin_Transaction: marks the beginning of a transaction execution; End_Transaction: specifies that the read and write operations have ended and
marks the end limit of transaction execution (but may be aborted because of concurrency control); Commit_Transaction: signals a successful end of the transaction. Any updates executed by the transaction can be safely committed to the database and will not be undone; Rollback (or Abort): signals that the transaction has ended unsuccessfully. Any changes that the transaction may have applied to the database must be undone; Undo: similar to ROLLBACK but it applies to a single operation rather than to a whole transaction; Redo: 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;
Transaction execution
A transaction reaches its commit point when all operations accessing the database are completed and the result has been recorded in the log. It then writes a [commit, transaction-id].
BEGIN TRANSACTION active END TRANSACTION partially committed ROLLBACK
COMMIT committed
READ, WRITE
ROLLBACK terminated
failed
If a system failure occurs, searching the log and rollback the transactions that have written into the log a [start_transaction, transaction-id] [write_item, transaction-id, X, old_value, new_value] but have not recorded into the log a [commit, transaction-id]
write_item(X):
writes the value of program variable X into the database item named X. 1. find the address of the disk block that contains item X 2. copy that disk block into a buffer in the main memory 3. copy item X from the program variable named X into its current location in the buffer store the updated block in the buffer back to disk (this step updates the database on disk)
X:=
data
log
1. Updates recorded in log 2. Transaction commit point 3. Force log to the disk 4. Update the database FAILURE! REDO database from log entries No UNDO necessary because database never altered
1. Update X recorded in log 2. Update X in database 3. Update Y recorded in log 4. Transaction commit point 3. Force log to the disk 4. Update Y in database
Undo in reverse order in log Redo in committed log order uses the write_item log entry
Simultaneous Execution
T1
Transfer 500 from A to B
T2
Transfer 300 from C to A
item X has incorrect value because its update from T1 is lost (overwritten) T2 reads the value of X before T1 changes it in the database and hence the updated database value resulting from T1 is lost
T2 reads X after N is subtracted and reads Y before N is added, so a wrong summary is the result
T2: (fred)
Database 4 2 2
Log old
Log new
Joe cancels
4 2 2 -1 -1 2 -1 rollback T1 log
transaction T1 fails and must change the value of X back to its old value meanwhile T2 has read the temporary incorrect value of X
Schedules of Transactions
A schedule S of n transactions is a sequential ordering of the operations of the n transactions.
The transactions are interleaved T1 read x write x T2 read x write x S read x read x write x write x
Two operations conflict if they belong to different transactions, AND access the same data item AND one of them is a write.
10
Schedule B
T1: T2: read_item(X); X:= X + M; write_item(X);
Schedule D
T1: read_item(X); X:= X - N; write_item(X); T2:
We have to figure out whether a schedule is equivalent to a serial schedule i.e. the reads and writes are in the right order
11
T1:
T2:
A schedule S is view serialisable if it is view equivalent to a serial schedule. Testing for view serialisability is NP-complete
12
Semantic Serialisability
Some applications can produce schedules that are correct but arent conflict or view serialisable. e.g. Debit/Credit transactions (Addition and subtraction are commutative)
T1 read_item(X); X:=X-10; write_item(X); read_item(Y); Y:=Y+10; write_item(Y); T2 read_item(Y); Y:=Y-20; write_item(Y); read_item(Z); Z:+Z+20; write_item(Z);
Schedule
T1 read_item(X); X:=X-10; write_item(X); T2
13
Types of Locks
Binary locks have two possible states:
1. locked (lock_item(X) operation) and 2. unlocked (unlock_item(X) operation
Multiple-mode locks allow concurrent access to the same item by several transactions. Three possible states:
1. read locked or shared locked (other transactions are allowed to read the item) 2. write locked or exclusive locked (a single transaction exclusively holds the lock on the item) and 3. unlocked.
14
10 10
15
read_lock(X); read_item(X); unlock(X); write_lock(Y); read_item(Y); Y:=X+Y; write_item(Y); unlock(Y); write_lock(X); read_item(X); X:=X+Y; write_item(X); unlock(X);
16
Two-Phasing Locking
Basic 2PL
When a transaction releases a lock, it may not request another lock lock point number of locks obtain lock release lock
Phase 1 BEGIN
Phase 2 END
Two-Phasing Locking
Strict 2PL a transaction does not release any of its locks until after it commits or aborts leads to a strict schedule for recovery
obtain lock number of locks release lock
BEGIN
Transaction duration
17
Deadlock detection (if the transaction load is light or transactions are short and lock only a few items)
wait-for graph for deadlock detection victim selection cyclic restarts
T1 T2
Livelock: a transaction cannot proceed for an indefinite period of time while other transactions in the system continue normally.
fair waiting schemes (i.e. first-come-first-served)
18
Locking Granularity
A database item could be
a database record a field value of a database record a disk block the whole database
Trade-offs
coarse granularity the larger the data item size, the lower the degree of concurrency fine granularity the smaller the data item size, the more locks to be managed and stored, and the more lock/unlock operations needed.
19
1 2 3 4 5 6
20
S hadowpagetab le (notupdated)
1 2 3 4 5 6
1 2 3 4 5 6
Commiting a transaction
discard previous shadow page free old page tables that it references
Garbage collection
21
Validation Phase
Use transaction timestamps write_sets and read_sets maintained Transaction B is committed or in its validation phase Validation Phase for Transaction A To check that TransA does not interfere with TransB the following must hold:
TransB completes its write phase before TransA starts its reads phase TransA starts its write phase after TransB completes its write phase, and the read set of TransA has no items in common with the write set of TransB Both the read set and the write set of TransA have no items in common with the write set of TransB, and TransB completes its read phase before TransA completes its read phase.
Conclusions
Transaction management deals with two key requirements of any database system: Resilience
in the ability of data surviving hardware crashes and software errors without sustaining loss or becoming inconsistent
Access Control
in the ability to permit simultaneous access of data multiple users in a consistent manner and assuring only authorised access
22