Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Relational model wikipedia , lookup
Database model wikipedia , lookup
Clusterpoint wikipedia , lookup
Global serializability wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Consistency model wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Commitment ordering wikipedia , lookup
Integrity Transactions: transaction concept, transaction state implementation of atomicity and durability concurrent executions serializability, recoverability implementation of isolation Ioan Despi 1 Transactions Transaction = a unit of program execution that accesses and possibly updates various data items. •A transaction must see a consistent database •During transaction execution the database may be inconsistent •When the transaction is committed, the database must be consistent The transaction concept is used in: 1. Database recovery 2. Concurrent control 2 ACID properties To preserve data integrity, the DBMS must ensure: Atomicity. Either all operations of the transaction are properly reflected in the database or none are. Consistency. Execution of a transaction in isolation preserves the consistency of the database. Isolation. Although multiple transactions may execute concurrently, each transaction must be un aware of other concurrently executing transactions. Intermediate transaction results must be hidden from other concurrently executed transactions. That is, for every pair of transactions T1and T2, it appears to T1 that either T2 finished execution before T1 started or T2 started execution after T1 finished Durability. After a transaction completes successfully, the changes it has made to the database persist, even if there are system failures. 3 Example. A transaction to transfer £100 from account A to account B. 1. Read (A) Consistency requirement: 2. A := A -100 the sum of A and B is unchanged by the 3. Write (A) execution of the transaction 4. Read (B) Atomicity requirement: 5. B := B + 100 if the transaction fails after step 3 and 6. Write (B) before step 6, the system should ensure that its updates are not reflected in the database, else an inconsistency will result 4 1. Read (A) Durability: Once the user has been notified that the transaction has completed (the transfer of 2. A := A -100 3. Write (A) £100 has taken place), the updates to the 4. Read (B) database by the transaction must persist 5. B := B + 100 despite failures. 6. Write (B) Isolation: if between steps 3 and 6 another transaction Can be ensured trivially by running transactions serially, one after the other is allowed to access the partially updated database, it will see an inconsistent one. (A + B will be less than it should be) 5 Transaction states Partially committed committed active failed aborted 6 Active: the initial state. The transaction stays in this state while it is executing Partially committed: After the final statement has been executed Failed: After the discovery that normal execution can no longer proceed Aborted: After the transaction has been rolled back and the database restored to its state prior to the start of the transaction --> 1.Restart the transaction 2. Kill the transaction Committed: After successful completion 7 Recovery - management component of DBMS implements the support for atomicity and durability by means of (for example) shadow-database scheme: - assume that only one transaction is active at a time - db_pointer always points to the current consistent copy of the database - all updates are made on a shadow copy of the database, and db_pointer is made to point to the updated shadow copy only after the transaction reaches partial commit and all updated pages have been flushed to the disk - in case transaction fails, old consistent copy pointed to by db_pointer can be used, and the shadow copy can be deleted 8 Shadow - database scheme: db_pointer db_pointer Old copy of database Before update Old copy of database (to be deleted) New copy of database After update •Assumes disks do not fail •useful for text editors, inefficient for large databases 9 Concurrent executions = Multiple transactions are allowed to run concurrently in the system Advantages: 1. Increased processor and disk utilization: one transaction can be using CPU while another is reading from or writing to the disk, leading to better throughput 2. Reduced average response time for transactions: short transactions need not wait behind long ones Concurrency control schemes = mechanisms to control the interaction among the concurrent transactions in order to prevent them from destroying the consistency of the database 10 Schedules = sequences that indicate the chronological order in which instructions of concurrent transactions are executed Requirements for a schedule for a set of transactions: 1. Must consist of all instructions of those transactions 2. Must preserve the order in which the instructions appear in each individual transaction Example: Let T1: transfer £100 from A to B and T2: transfer 10% of the balance from A to B 11 Schedule 1. Serial, T1 is followed by T2 T1 T2 read (A) A := A - 100 write (A) read (B) B := B + 100 write (B) read (A) temp := A * 0.1 A := A - temp write (A) read (B) B := B + temp write (B) 12 Schedule 2. Not serial, but equivalent to schedule 1 T1 T2 read (A) A := A - 100 write (A) read (A) temp := A * 0.1 A := A - temp write (A) read (B) B := B + 100 write (B) read (B) B := B + temp write (B) 13 Schedule 3. Not serial, does not preserve the sum A + B T1 T2 read (A) A := A - 100 read (A) temp := A * 0.1 A := A - temp write (A) read (B) write (A) read (B) B := B + 100 write (B) B := B + temp write (B) 14 Serializability Basic assumption: Each transaction preserves DB consistency Serial execution of a set of transactions preserves DB consistency Definition. A (possibly concurrent) schedule is serializable if it is equivalent to a serial execution For our purposes: we will ignore operations other than read and write assume that transactions may perform arbitrary computations on data in local buffers between reads and writes 15 Different forms of schedule equivalence gives rise to the notions of conflict serializability and view serializability. 1. Conflict serializability Definition. Instructions a and b, of T1 and T2 respectively, conflict if and only if there exists some item Q accessed by both a and b and at least one of these instructions wrote Q 1. a = read(Q), b =read(Q) ==> a and b don’t conflict 2. a = read(Q), b = write(Q) ==> conflict 3. a = write(Q), b = read(Q) ==> conflict 4. a = write(Q), b = write(Q) ==> conflict 16 Intuitively: A conflict between a and b forces a temporal order between them If a and b are consecutive in a schedule and they do not conflict their results would remain the same even if they had been interchanged in the schedule. Definition. If a schedule S can be transformed into a schedule S’ by a series of swaps of non-conflicting instructions, then S and S’ are conflict equivalent. Definition. A schedule S is conflict serializable if it is conflict equivalent to a serial schedule. 17 Example. S2 T1 T2 S1 T1 Read(A) Read(A) Write(A) Write(A) Read(A) Read(A) Write(A) Write(A) T2 Read(B) Read(B) Write(B) Write(B) Read(B) Read(B) Write(B) Write(B) Schedule S2 can be transformed into schedule S1, a serial schedule, where T2 follows T1, by a series of swaps of nonconflicting instructions. Therefore, S2 is conflict serializable. 18 Counterexample. S3 T3 T4 Read(A) Write(A) Write(A) We are unable to swap instructions in the above schedule to obtain either the serial schedule {T3, T4} or the serial schedule {T4, T3}. Therefore, schedule S3 is not conflict serializable. 19 2. View serializability Definition. Let S and S’ be two schedules with the same set of transactions. S and S’ are view equivalent if the following 1. For each data item Q, if the transaction T1 reads the initial value of Q in schedule S, then transaction T1 must read the initial value of Q in schedule S’. 2. For each data item Q, if the transaction T1 executes read(Q) in schedule S, and that value was produced by transaction T2 (if any) then transaction T1 must in schedule S’ also read the value of Q that was produced by transaction T2 3. For each data item Q, the transaction (if any) that performs the final write(Q) operation in schedule S must perform the final write(Q) operation in schedule S’. hold. 20 Obs. View equivalence is also based purely on reads and writes alone. Definition. A schedule S is view serializable if it is view equivalent to a serial schedule. Every conflict serializable schedule is also view serializable Every view serializable schedule which is not conflict serializable has blind writes. T3 T4 T5 Example of schedule which is view serializable but not conflict serializable. Read(Q) Write(Q) Write(Q) Write(Q) S4 21 T7 T8 Other notions of serializability Read(A) A := A – 100 Write(A) Read(B) B := B –10 Write(B) Read(B) B := B + 100 Schedule S5 produces the same outcome as the serial schedule {T7, T8}, yet is not conflict equivalent or Write(B) Read(A) view equivalet to it. A :=A + 10 Write(A) S5 Determining such equivalencies requires analysis of operations other than read and write. 22 Recoverability Need to address the effect of transaction failures on concurrent transactions Recoverable schedule = if a transaction T1 reads a data items previously written by a transaction T2, the commit operation of T2 appears before the commit operation of T1 T1 T2 Read(A) Write(A) Read(A) Read(B) This schedule is not recoverable if T2 commits immediately after the read: if T1 should abort, T2 would have read an inconsistent database state. Hence database must ensure that schedules are recoverable. 23 Cascading rollback: a single transaction failure leads to a series of transaction rollbacks. Consider the following schedule where none of the transactions has yet committed (so the schedule is recoverable): T13 T14 T15 Read(a) If T13 fails, T14 and T15 mulst also be rolled back Read(b) Write(a) this can lead to the undoing of a significant amount of work Read(a) Write(a) Read(a) 24 Cascadeless schedules = cascading rollbacks cannot occur: for each pair of transactions T1 and T2 such that T2 reads a data item previously written by T1, the commit operation of T1 appears before the read operation of T2 •Every cascadeless schedule is also recoverable • It is desirablee to restrict the schedules to those that are cascadeless 25 Implementation of isolation •Schedules must be conflict or view serializable and recoverable, for the sake of database consistency, and preferably cascadeless •A policy in which only one transaction can execute at a time generates serial schedules, but provides a poor degree of concurrency •Concurrency-control schemes tradeoff between the amount of concurrency they allow and the amount of overhead that they incur. •Some schemes allow only conflict-serializable schedules to be generated, while others allow view-serializable schedules that are not conflict-serializable 26