Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Transactions 7.1 Transaction Concept 7.2 Transaction State 7.3 Implementation of Atomicity and Durability 7.4 Concurrent Executions 7.5 Serializability.

Similar presentations


Presentation on theme: "Chapter 7 Transactions 7.1 Transaction Concept 7.2 Transaction State 7.3 Implementation of Atomicity and Durability 7.4 Concurrent Executions 7.5 Serializability."— Presentation transcript:

1 Chapter 7 Transactions 7.1 Transaction Concept 7.2 Transaction State 7.3 Implementation of Atomicity and Durability 7.4 Concurrent Executions 7.5 Serializability 7.6 Recoverability 7.7 Testing for Serializability

2 7.1 Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data items. Properties of the transactions: Atomicity Consistency Isolation Durability

3 It can be verified easily that, if the database is consistent before an execution of the transaction, the database remains consistent after the execution of the transaction. 7.1 Transaction Concept Consistency. Execution of a transaction in isolation(that is, with no other transaction executing concurrently) preserves the consistency of the database.

4 Atomicity. Either all operations of the transaction are reflected properly in the database, or none are. Transaction-management component 7.1 Transaction Concept A=$950 B=$2000 A+B= $2950 X A=$100 B=$2000 A+B= $2000 Read (A); $1000 A:=A-$50; Write (A); $950 Ti: Read (B); $2000 B:=B+$50; Write (B); $2050 inconsist ent state

5 Durability. After a transaction completes successfully, the changes it has made to the database persist, even if there are system failures. Recovery-management component. 7.1 Transaction Concept we can guarantee durability by ensuring that either: ① The updates carried out by the transaction have been written to disk before the transaction completes. ② Information about the updates carried out by the transaction and written to disk is sufficient to enable the database to reconstruct the updates when the database system is restarted after the failure.

6 Isolation. Even though multiple transactions may execute concurrently, the system guarantees that, for every pair of transactions T i and T j, it appears to T i that either T j finished execution before T i started, of T j started execution after T i finished. Thus, each transaction is unaware of other transactions executing concurrently in the system. Concurrency-control component. 7.1 Transaction Concept

7 <> 7.1 Transaction Concept Read (A); $1000 A:=A-$50; Write (A); $950 Transaction T i Transaction T j Read (A); $1000 A:=A-$50; Write (A); $950 $900 ? Lost Update:

8 7.1 Transaction Concept C:=A+B; $5000 Transaction T i Read (A); $1000 Read (B); $2000 C:=A+B; $3000 Read (A); $1000 Read (B); $4000 Transaction T j Write (B); $4000 Read (B); $2000 B:=B*2; ? Not-Repeatable Read:

9 7.1 Transaction Concept Transaction T j Read (C); $4000 Write (C); $4000 Read (C); $2000 C:=C*2; Transaction T i ROLLBACK; C:=$2000 ? Dirty Read:

10 Aborted:a transaction that not complete its execution successfully. Rolled back:the changes caused by an aborted transaction have been undone. Compensation transaction:the only way to undo the effects of a committed transaction is to execute a compensation transaction. T i : add &20 to an account compensation T j : subtract $20 from the account. 7.2 Transaction State

11 Partially committed failed aborted active after final statement has been executed after successful completion after transaction roll back & the db restore after the discovery that normal execution can no longer proceed the initial state write out information to disk hardware failure or logical errors system has two options:restart or kill

12 Shadow copy scheme: ① only one transaction is active at a time. ② the database is simply a file on disk. A pointer called db-pointer is maintained on disk; it points to the current copy of the database. 7.3 implementation of Atomicity and Durability old copy of database (to be deleted) old copy of database new copy of database db-pointer After updateBefore update ① ② ③

13 T 1 read(A) A:=A-50 write(A) read(B) B:=B+50 write(B) 7.4 Concurrent Executions T 2 read(A) temp:=A*0.1 A:=A-temp write(A) read(B) B:=B+temp write(B) T 1 read(A) A:=A-50 write(A) read(B) B:=B+50 write(B) T 2 read(A) temp:=A*0.1 A:=A-temp write(A) read(B) B:=B+temp write(B) 950 1000 2000 2050 950 855 2050 2145 1000 900 2000 2100 900 850 2100 2150

14 T 1 read(A) A:=A-50 write(A) read(B) B:=B+50 write(B) 7.4 Concurrent Executions T 2 read(A) temp:=A*0.1 A:=A-temp write(A) read(B) B:=B+temp write(B) T 1 read(A) A:=A-50 write(A) read(B) B:=B+50 write(B) T 2 read(A) temp:=A*0.1 A:=A-temp write(A) read(B) B:=B+temp write(B) 950 1000 2000 2050 950 855 2050 2145 1000 950 2000 2050 1000 900 2000 2100 A+B=3000 A+B=3050 ?

15 7.5.1 Conflict Serializability a schedule S has two consecutive instructions I i and I j, of transactions T i and T j, respectively(i<>j).if I i and I j refer to different data items, then we can swap I i and I j without affecting the results of any instruction in the schedule. However, if I i and I j refer to the same data item Q, then the order of the two steps may matter. There are four cases that we need to consider: 7.5 Serializability

16 1.I i =read(Q), I j =read(Q). The order of I i and I j does not matter, since the same value of Q is read by T i and T j, regardless of the order. 2.I i =read(Q), I j =write(Q). If I i comes before I j, then T i does not read the value of Q that is written by T j in instruction I j. If I j comes before I i, then T i reads the value of Q that is written by T j. Thus, the order of I i and I j matters. 1.I i =write(Q), I j =read(Q). The order of I i and I j matters for reasons similar to those of the previous case. 3.I i =write(Q), I j =write(Q). Since both instructions are write operations, the order of these instructions does not affect either T i or T j. However, the values obtained by the next read(Q) instruction of S is affected, since the result of only the latter of the two write instructions is preserved in the database. 7.5 Serializability

17 We say that I i and I j conflict if they are operations by different transactions on the same data item, and at least one of these instructions is a write operation. 7.5 Serializability T 1 read(A) write(A) read(B) write(B) T 2 read(A) write(A) read(B) write(B) T 1 read(A) write(A) read(B) write(B) T 2 read(A) write(A) read(B) write(B)

18 Conflict equivalent: if a schedule S can be transformed into a schedule S’ by a series of swaps of non- conflicting instructions, we say that S and S’ are conflict equivalent. Conflict serializable: if a schedule S is conflict equivalent to the serial schedule. 7.5 Serializability not conflict serializable T 3 read(Q) write(Q) T 4 write(Q) schedule7

19 7.5 Serializability T 1 read(A) A:=A-50 write(A) read(B) B:=B+50 write(B) T 5 read(B) B:=B-10 write(B) read(A) A:=A+10 write(A) Example: conflicts 950 1000 1990 2040 2000 1990 950 960 A+B=3000 There are less stringent definitions of schedule equivalence than conflict equivalence.

20 The schedules S and S’ are said to be view equivalent if three conditions are met: 1. For each data item Q, if transaction T i reads the initial values of Q in schedule S, then transaction T i must, inschedule S’, also read the initial value of Q. 2. For each data item Q, if transaction Ti executes read(Q) in schedule S, and if that value was produced by a write(Q) operation of transaction T j, then the read(Q) operation of transaction T i must, in schedule S’, also read the value of Q that was produced by the same write(Q) operation of transaction T j. 3. For each data item Q, the transaction that performs the final write(Q) operation in schedule S must perform the final write(Q) operation in schedule S’. 7.5.2 View Serializability

21 View serializable: if a schedule S is view equivalent to a serial schedule. 7.5 Serializability equvalent to the serial schedule view serializable T 3 read(Q) write(Q) T 4 write(Q) T 6 write(Q) Schedule 9 blind write

22 7.6 Recoverability commit immediately T 8 fails before it commits T 8 rollback:A(5) T 8 read(A) A:=A+1 write(A) read(B) 5 6 T 9 read(A) 6 Recoverable schedule: for each pair of transactions T i and T j such that T j reads a data item previously written by T i, the commit operation of T i appears before the commit operation of T j T i →T 8 T j →T 9

23 7.6 Recoverability T 10 read(A) read(B) write(A) T 11 read(A) write(A) T 12 read(A) T 10 fails rollback T 11 is dependent on T 10, rollback T 12 is dependent on T 11, rollback Cascading rollback Cascadeless schedule: for each pair of transactions T i and T j such that T j reads a data item previously written by T i, the commit operation of T i appears before the read operation of T j

24 Precedence graph G=(V,E) V: a set of vertices consists of all the transactions participating in the schedule. E: a set of edges consists of all edges T i →T j for which one of three conditions holds: 1.T i executes write(Q) before T j executes read(Q). 2.T i executes read(Q) before T j executes write(Q). 3.T i executes write(Q) before T j executes write(Q). 7.7 Testing for Serializability

25 contain cycles not conflict serializable 7.7 Testing for Serializability T 1 read(A) A:=A-50 write(A) read(B) B:=B+50 write(B) T 2 read(A) temp:=A*0.1 A:=A-temp write(A) read(B) B:=B+temp write(B) T 1 read(A) A:=A-50 write(A) read(B) B:=B+50 write(B) T 2 read(A) temp:=A*0.1 A:=A-temp write(A) read(B) B:=B+temp write(B) T1T1 T2T2 T1T1 T2T2 T 1 → T 2 T 2 → T 1 not contain cycles conflict serializable


Download ppt "Chapter 7 Transactions 7.1 Transaction Concept 7.2 Transaction State 7.3 Implementation of Atomicity and Durability 7.4 Concurrent Executions 7.5 Serializability."

Similar presentations


Ads by Google