
2a9474125c64a9e2418f9c993bfdca0f.ppt
- Количество слайдов: 44
Transaction 02/23/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Outline n n Transaction and OLTP Desired properties of transactions Schedule Concurrent transactions and their properties 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Transaction n A multi-billion dollar business OLTP http: //www. tpc. org/default. asp 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Transaction Definition n n A unit of program execution that accesses and possibly updates various data items Transactions? ¡ ¡ ¡ ¡ Book an airline ticket from DFW to Paris Buy “The Dilbert Principle” from amazon. com Sell 1000 shares of LU from your ameritrade account Withdraw $100 from a ATM machine Issue a SQL statement to sqlplus of Oracle 9 i Look at your grade of homework 3 Check out your groceries at Walmart 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Transaction Concept n A transaction is a unit of program execution that accesses and possibly updates various data items. a 1, a 2, a 3, a 4, …, an, commit consistent n Database may be inconsistent Two main issues to deal with: ¡ ¡ Failures of various kinds, such as hardware failures and system crashes Concurrent execution of multiple transactions a 1, a 2, a 3, a 4, …, an, commit b 1, b 2, b 3, b 4, …, bm, commit c 1, c 2, c 3, c 4, …, cl, commit 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Challenges to Maintain Transactions n Hardware failures ¡ ¡ n Software failures ¡ ¡ n Programming errors System crash… User interference ¡ n Cashed stuck in ATM machine Power failure… Termination of transactions Concurrent users ¡ Multiple users accessing the same item 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Designed Properties of Database Systems n n Atomicity Consistency Isolation Durability 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Atomicity n n Transaction needs to be executed as a unit Example ¡ n You should not cause the quantity of “The Dilbert Principle” of amazon. com decrease if you place your order and the order does not get through due to server errors Who are responsible for atomicity? ¡ ¡ Transaction management system and Recovery system 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Example of Fund Transfer n Transaction to transfer $50 from account A to account B: 1. 2. 3. 4. 5. 6. 7. 1/14/2005 read(A) A : = A – 50 write(A) read(B) B : = B + 50 write(B) commit Yan Huang - CSCI 5330 Database Implementation – Transaction
Atomicity n Either all operations of the transaction are properly reflected (1) read(A), (2)A : = A -50, (3)write(A), (4) read(B), (5)B : = B + 50, (6)write(B), (7) commit n Or none are 1/14/2005 (1) read(A), (2)A : = A -50, (3)write(A), (4) read(B), (5)B : = B + 50 Yan Huang - CSCI 5330 Database Implementation – Transaction
Consistency n n Database implicit/explicit constraints need to be maintained Example: ¡ n Transferring money from one account to another in the same bank should not change your total amount of money Who are responsible for consistency? ¡ ¡ Transaction management system and Programmer 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Consistency n A+B = TOT where TOT is a constant value (1) read(A), (2)A : = A -50, (3)write(A), (4) read(B), (5)B : = B + 50, (6)write(B), (7) commit A+B= TOT n A+B may not equal to TOT A+B= TOT Consistency: DB satisfies all integrity and constraints ¡ Examples: n - x is key of relation R - x y holds in R - Domain(x) = {Red, Blue, Green} - a is valid index for attribute x of R no employee should make more than twice the average salary n A+B = TOT n n 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Isolation n n Transaction A should not see partial results of transaction B Analogy: ¡ n When I update my website here and there, you should not see and think a tentative version as my final version Who are responsible for isolation? ¡ Transaction management system 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Isolation n Intermediate transaction results must be hidden from other concurrently executed transactions. A+B ≠ TOT? ! T 2 (1) read(A), (2)A : = A -50, (3)write(A), (4) read(B), (5)B : = B + 50, (6)write(B), (7) commit A+B= TOT 1/14/2005 A+B may not equal to TOT Yan Huang - CSCI 5330 Database Implementation – Transaction A+B= TOT
Durability n n Any transaction committed needs to be in database for ever Example: ¡ After you get the receipt of the water melon you buy from Alberson, the transaction is final and permanently reflected in the database system n n If you want to cancel it, that is another transaction Who are responsible for durability? ¡ ¡ Transaction management system and Recovery system 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Durability n After a transaction completes successfully, the changes it has made to the database persist, even if there are system failures. (1) read(A), (2)A : = A -50, (3)write(A), (4) read(B), (5)B : = B + 50, (6)write(B), (7) commit After this point, A and B are permanently updated 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Transaction State (Cont. ) a 1, a 2, a 3, a 4, …, an, 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction commit
An Ideal World n n No hardware failures No software failures No programming errors Do we still need transaction management? 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Why Concurrent Transactions? n n Parallelism Improved response time 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Storage Hierarchy n n Read(x) read x from memory, if it is not in memory yet, read from disk first Write(x) writes x to memory and possibly to disk 1. 2. 3. 4. 5. 6. 7. read(A) A : = A – 50 write(A) read(B) B : = B + 50 write(B) commit x Memory 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction x Disk
Schedules T 1 T 2 Read(A) A: =A-50 Temp: =A*0. 1 Write(A) A: =A-temp Read(B) Write(A) B: =B+50 Read(B) Write(B) B: =B+temp Write(B) T 1 transfer $50 from A to B T 2 transfer 10% of the balance from A to B 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction Schedule 1 Read(A) A: =A-50 Read(A) Temp: =A*0. 1 A: =A-temp Write(A) Read(B) B: =B+50 Write(B) B: =B+temp Write(B)
Schedules n Schedules – sequences that indicate the chronological order in which instructions of concurrent transactions are executed ¡ ¡ a schedule for a set of transactions must consist of all instructions of those transactions must preserve the order in which the instructions appear in each individual transaction. 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Serial Schedule n T 1 is followed by T 2. Schedule 2 Read(A) A: =A-50 Write(A) Read(B) B: =B+50 Write(B) Read(A) Temp: =A*0. 1 A: =A-temp Write(A) Read(B) B: =B+temp Write(B) 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction A = 100, B = 100 originally A = ? and B = ?
Example Schedule (Cont. ) n Schedule 3 is equivalent to Schedule 1. Schedule 3 In both Schedule 2 and 3, the sum A + B is preserved. 1/14/2005 Read(A) A: =A-50 Write(A) Read(A) Temp: =A*0. 1 A: =A-temp Write(A) Read(B) B: =B+50 Write(B) Read(B) B: =B+temp Write(B) Yan Huang - CSCI 5330 Database Implementation – Transaction A = 100, B = 100 originally A = ? and B = ?
Example Schedules (Cont. ) Schedule 4 does not preserve the sum A + B 1/14/2005 Read(A) A: =A-50 Read(A) Temp: =A*0. 1 A: =A-temp Write(A) Read(B) B: =B+50 Write(B) B: =B+temp Write(B) Yan Huang - CSCI 5330 Database Implementation – Transaction A = 100, B = 100 originally A = ? and B = ?
Where is the mystery? n How to preserve database consistency? Serializability! 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Serializability n A (possibly concurrent) schedule is serializable if it is equivalent to a serial schedule. 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Conflict Serializability n n Transactions T 1 and T 2 Two operations on the same item Q, Conflict? n n Intuitively, a conflict between T 1 and T 2 forces a (logical) temporal order between T 1 and T 2 T 1 Read(Q) Write(Q) Two consecutive non-conflict operations in a schedule can been interchanged 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction Read(Q) Write(Q)
Conflict Serializability (Cont. ) n If a schedule S can be transformed into a schedule S´ by a series of swaps of nonconflicting instructions, we say that S and S´ are conflict equivalent. 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Note n n Only read and write operations will cause conflict Other operations (A: =A+10) are on local copy variables and do not interface with database 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Simplified Schedules Schedule 3 Schedule 2 Read(A) A: =A-50 Write(A) Read(A) Write(A) Temp: =A*0. 1 Read(B) A: =A-temp Write(B) Write(A) Read(B) Write(B) B: =B+50 Write(B) Read(B) B: =B+temp Write(B) Read(A) A: =A-50 Write(A) Read(B) Write(B) B: =B+50 Read(A) Write(B) Write(A) Read(B) Temp: =A*0. 1 Write(B) A: =A-temp Write(A) Read(B) B: =B+temp Write(B) 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Schedule 3 and Schedule 2 are conflict equivalent Schedule 3 Schedule 2 Read(A) Write(A) Read(B) Write(B) 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Schedule 3 and Schedule 2 are conflict equivalent Schedule 3 Schedule 2 Read(A) Write(A) Read(B) Write(A) Write(B) Read(B) Write(B) Read(A) Write(A) Read(B) Write(B) 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Schedule 3 and Schedule 2 are conflict equivalent Schedule 3 Schedule 2 Read(A) Write(A) Read(B) Write(B) Read(A) Write(A) Read(B) Write(B) 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Schedule 3 and Schedule 2 are conflict equivalent Schedule 3 Schedule 2 Read(A) Write(A) Read(B) Read(A) Write(B) Write(A) Read(B) Write(B) Read(A) Write(A) Read(B) Write(B) 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Schedule 3 and Schedule 2 are conflict equivalent Schedule 3 Schedule 2 Read(A) Write(A) Read(B) Write(B) 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Conflict Serializability (Cont. ) n n We say that a schedule S is conflict serializable if it is conflict equivalent to a serial schedule Schedule 3 is conflict serializable 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Conflict Serializability (Cont. ) n Example of a schedule that is not conflict serializable: T 3 T 4 read(Q) write(Q) We are unable to swap instructions in the above schedule to obtain either the serial schedule < T 3, T 4 >, or the serial schedule < T 4, T 3 >. 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Testing for Serializability n Precedence graph — a direct graph where the vertices are the transactions (names). 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Example Schedule (Schedule A) T 1 T 4 T 2 T 3 T 5 read(X) read(Y) read(Z) read(V) read(W) read(Y) write(Z) read(U) 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction read(U) read(Y) write(Y) read(Z) write(Z)
Precedence Graph for Schedule A T 1 T 2 T 5 T 3 1/14/2005 T 4 Yan Huang - CSCI 5330 Database Implementation – Transaction
Recoverability n Only commit after the transaction your read from commits 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Cascadeless Schedule n Only read committed write 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
Check Schedules Schedule 1 Schedule 2 Schedule 3 Schedule 4 Read(A) Write(A) Read(B) Write(B) Read(A) Write(A) Read(B) Write(B) Conflict serializable? Recoverable? Cascadeless? 1/14/2005 Yan Huang - CSCI 5330 Database Implementation – Transaction
2a9474125c64a9e2418f9c993bfdca0f.ppt