1、Transaction Processing Recovery & Concurrency Control,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 ( A collection of actions that make consistent tr
2、ansformations of system states while preserving system consistency An indivisible unit of processing,database in a consistent state,database in a consistent state,database may be temporarily in an inconsistent state during execution,begin Transaction,end Transaction,execution of Transaction,Account
3、A Fred Bloggs 1000,Account B Sue Smith 0,Account B Sue Smith 500,Account A Fred Bloggs 500,Transfer 500,Desirable Properties of ACID Transactions,A Atomicity: a transaction is an atomic unit of processing and it is either performed entirely or not at all C Consistency Preservation: a transactions co
4、rrect execution must take the database from one correct state to another I Isolation/Independence: the updates of a transaction must not be made visible to other transactions until it is committed (solves the temporary update problem) D Durability (or Permanency): if a transaction changes the databa
5、se and is committed, the changes must never be lost because of subsequent failure o Serialisability: transactions are considered serialisable if the effect of running them in an interleaved fashion is equivalent to running them serially in some order,Requirements for Database Consistency,Concurrency
6、 Control Most DBMS are multi-user systems. The concurrent execution of many different transactions submitted by various users must be organised such that each transaction does not interfere with another transaction with one another in a way that produces incorrect results. The concurrent execution o
7、f transactions must be such that each transaction appears to execute in isolation. Recovery System failures, either hardware or software, must not result in an inconsistent database,Transaction as a Recovery Unit,If an error or hardware/software crash occurs between the begin and end, the database w
8、ill be inconsistent Computer Failure (system crash) A transaction or system error Local errors or exception conditions detected by the transaction Concurrency control enforcement Disk failure Physical problems and catastrophes The database is restored to some state from the past so that a correct st
9、ateclose 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) ens
10、ure Transaction Atomicity,Recovery,Mirroring keep two copies of the database and maintain them simultaneouslyBackup periodically dump the complete state of the database to some form of tertiary storageSystem Logging the log keeps track of all transaction operations affecting the values of database i
11、tems. The log is kept on disk so that it is not affected by failures except for disk and catastrophic failures.,Recovery from Transaction Failures,Catastrophic failure Restore a previous copy of the database from archival backup Apply transaction log to copy to reconstruct more current state by redo
12、ing committed transaction operations up to failure point Incremental dump + log each transaction 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 consult
13、ed 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 t
14、hat 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
15、 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 certai
16、n transaction operations must be redone to ensure that all the operations of a committed transaction have been applied successfully to the database;,Entries in the System Log,For every transaction a unique transaction-id is generated by the system. start_transaction, transaction-id: the start of exe
17、cution of the transaction identified by transaction-idread_item, transaction-id, X: the transaction identified by transaction-id reads the value of database item X. Optional in some protocols. write_item, transaction-id, X, old_value, new_value: the transaction identified by transaction-id changes t
18、he value of database item X from old_value to new_valuecommit, transaction-id: the transaction identified by transaction-id has completed all accesses to the database successfully and its effect can be recorded permanently (committed)abort, transaction-id: the transaction identified by transaction-i
19、d has been aborted,Credit_labmark (sno NUMBER, cno CHAR, credit NUMBER) old_mark NUMBER; new_mark NUMBER; SELECT labmark INTO old_mark FROM enrol WHERE studno = sno and courseno = cno FOR UPDATE OF labmark; new_ mark := old_ mark + credit;UPDATE enrol SET labmark = new_mark WHERE studno = sno and co
20、urseno = cno ;COMMIT;EXCEPTION WHEN OTHERS THEN ROLLBACK;END credit_labmark;,active,partially,committed,committed,failed,terminated,BEGIN,TRANSACTION,READ,WRITE,END,TRANSACTION,ROLLBACK,ROLLBACK,COMMIT,Transaction execution,A transaction reaches its commit point when all operations accessing the dat
21、abase are completed and the result has been recorded in the log. It then writes a commit, transaction-id.,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
22、 but have not recorded into the log a commit, transaction-id,Read and Write Operations of a Transaction,Specify read or write operations on the database items that are executed as part of a transaction read_item(X): reads a database item named X into a program variable also named X. 1. find the addr
23、ess 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 buffer to the program variable named 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 i
24、tem 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,X:=,Checkpoints in the System Log,A checkpoint record
25、is written periodically into the log when the system writes out to the database on disk the effect of all WRITE operations of committed transactions. All transactions whose commit, transaction-id entries can be found in the system log will not require their WRITE operations to be redone in the case
26、of a system crash. Before a transaction reaches commit point, force-write or flush the log file to disk before commit transaction. Actions Constituting a Checkpoint temporary suspension of transaction execution forced writing of all updated database blocks in main memory buffers to disk writing a ch
27、eckpoint record to the log and force writing the log to disk resuming of transaction execution,data,log,“In place” updating protocols: Overwriting data in situ,Deferred Update:no actual update of the database until after a transaction reaches its commit point 1. Updates recorded in log 2. Transactio
28、n commit point 3. Force log to the disk 4. Update the database,Immediate Update:the database may be updated by some operations of a transaction before it reaches its commit point. 1. Update X recorded in log 2. Update X in database 3. Update Y recorded in log 4. Transaction commit point 3. Force log
29、 to the disk 4. Update Y in database,FAILURE! REDO database from log entries No UNDO necessary because database never altered,FAILURE! UNDO X,FAILURE! REDO Y,Undo in reverse order in logRedo in committed log orderuses the write_item log entry,Write Ahead Logging,Transaction as a Concurrency Unit,Tra
30、nsactions must be synchronised correctly to guarantee database consistency,Account A Fred Bloggs 1000,Account B Sue Smith 0,Account B Sue Smith 500,Account A Fred Bloggs 500,Transfer 500 from A to B,Account C Jill Jones 700,Account C Jill Jones 400,Account A Fred Bloggs 800,Transfer 300 from C to A,
31、Net result Account A 800 Account B 500 Account C 400,T1,T2,Simultaneous Execution,Transaction scheduling algorithms,Transaction Serialisability The effect on a database of any number of transactions executing in parallel must be the same as if they were executed one after anotherProblems due to the
32、Concurrent Execution of Transactions The Lost Update Problem The Incorrect Summary or Unrepeatable Read Problem The Temporary Update (Dirty Read) Problem,The Lost Update Problem,Two transactions accessing the same database item have their operations interleaved in a way that makes the database item
33、incorrectitem 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,X=4 Y=8 N=2 M=3,The Incorrect Summary or Unrepeatable Read Problem,One transaction is cal
34、culating 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.,T2 reads X after N is subtracted and reads Y before N is added, so a wrong summary is
35、the result,Dirty Read or The Temporary Update Problem,One transaction updates a database item and then the transaction fails. The updated item is accessed by another transaction before it is changed back to its original valuetransaction T1 fails and must change the value of X back to its old value m
36、eanwhile T2 has read the “temporary” incorrect value of X,Joe books seat on flight X,Fred books seat on flight X because Joe was on Flight X,Joe cancels,Schedules of Transactions,A schedule S of n transactions is a sequential ordering of the operations of the n transactions. The transactions are int
37、erleaved A schedule maintains the order of operations within the individual transaction. For each transaction T if operation a is performed in T before operation b, then operation a will be performed before operation b in S. The operations are in the same order as they were before the transactions w
38、ere interleaved Two operations conflict if they belong to different transactions, AND access the same data item AND one of them is a write.,read x write x,read x write x,read x read x write x write x,T1,T2,S,Serial and Non-serial Schedules,A schedule S is serial if, for every transaction T participa
39、ting in the schedule, all of Ts operations are executed consecutively in the schedule; otherwise it is called non-serial. Non-serial schedules mean that transactions are interleaved. There are many possible orders or schedules. Serialisability theory attempts to determine the correctness of the sche
40、dules. A schedule S of n transactions is serialisable if it is equivalent to some serial schedule of the same n transactions.,Schedule B,Example of Serial Schedules,Schedule A,Example of Non-serial Schedules,Schedule C,Schedule D,We have to figure out whether a schedule is equivalent to a serial sch
41、edule i.e. the reads and writes are in the right order,Precedence graphs (assuming read X before write X),View Equivalence and View Serialisability,View Equivalence: As long as each read operation of a transaction reads the result of the same write operation in both schedules, the write operations o
42、f each transaction must produce the same results. The read operations are said to see the same view in both schedules The final write operation on each data item is the same in both schedules, so the database state should be the same at the end of both schedules A schedule S is view serialisable if
43、it is view equivalent to a serial schedule. Testing for view serialisability is NP-complete,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),Schedule,Me
44、thods for Serialisability,Multi-version Concurrency Control techniques keep the old values of a data item when that item is updated. Timestamps are unique identifiers for each transaction and are generated by the system. Transactions can then be ordered according to their timestamps to ensure serial
45、isability. Protocols that, if followed by every transaction, will ensure serialisability of all schedules in which the transactions participate. They may use locking techniques of data items to prevent multiple transactions from accessing items concurrently. Pessimistic Concurrency Control Check bef
46、ore a database operation is executed by locking data items before they are read and written or checking timestamps,Locking Techniques for Concurrency Control,The concept of locking data items is one of the main techniques used for controlling the concurrent execution of transactions. A lock is a var
47、iable associated with a data item in the database. Generally there is a lock for each data item in the database. A lock describes the status of the data item with respect to possible operations that can be applied to that item. It is used for synchronising the access by concurrent transactions to th
48、e database items. A transaction locks an object before using it When an object is locked by another transaction, the requesting transaction must wait,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
49、 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. Locks are held in a lock table. upgrade lock: read lock to write lock downgrade lock: write lock to read lock,Locks dont guarantee serialisability: Lost Update,Y is unlocked too early,X is unlocked too early,