Transactions and concurrency
Introduction to databases CSCC43 Winter 2013 Ryan Johnson
Thanks to Arnold Rosenbloom and Renee Miller for material in these slides
Transactions and concurrency Introduction to databases CSCC43 - - PowerPoint PPT Presentation
Transactions and concurrency Introduction to databases CSCC43 Winter 2013 Ryan Johnson Thanks to Arnold Rosenbloom and Renee Miller for material in these slides 2 Transactions: an old idea Database models business transactions
Thanks to Arnold Rosenbloom and Renee Miller for material in these slides
2
3
4
5
// C language int do_something(int input) { void* ptr = malloc(input); int err = do_some_work(ptr); if(err) { free(ptr); // never mind return err; } err = do_some_more_work(); if(err) { // undo some_work? // free ptr? return err; } return 0; } // Java void do_something(int input) { char[] tmp = new char[input]; do_some_work(buf); try { do_some_more_work(); } catch(Exception e) { // undo some_work? throw; } } // Transactional void do_something(int input) { char[] tmp = new char[input]; do_some_work(buf); do_some_more_work(); }
– E.g. wrap two smaller transactions in an outer one
– E.g. searching a linked list uses only 1-2 nodes at a time … but transaction remembers all nodes (bad for concurrency)
– Fire missiles, dispense cash, ship package, overwrite file, …
– Irrelevant: no need to undo (e.g. temp variables) – Independent: should not undo (e.g. caching)
6
7
8
9
10
Time
11
Time
12
Process A Process B x = 10 x = 20 x += 5 print x “15” print x “20”
13
P – Write after Write (WAW) A – dirty write w1(x) w2(x) ... c2 c1 Lost committed write by T1 P – Read after Write (RAW) A1 – dirty read w1(x) r2(x) ... [a1 and c2] T2 uses rolled-back value of x A2 – read skew r1(x) w2(x) w2(y) c2 r1(y) T1 sees old x and new y P – Write after Read (WAR) A1 – fuzzy read r1(x) w2(x) ... r1(x) Value of x not stable during xct A2 – lost update r1(x) w2(x) ... w1(x) T1’s update depends on stale x A3 – phantom r1(P) w2(add x to P) ... c2 P as originally read is missing x P – snapshot-related A1 – write skew r1(x) r2(y) w1(y) w2(x) [c1 and c2] Two updates cross in flight A2 – read-only r2(x) r1(y) w1(y) c1 r3(x) r3(y) c3 w2(x) c2 T3 sees mix of past/future data
14
– You make offer on a house but the Realtor waits several days before responding that somebody else made a higher offer.
– Your friend invites you to go with her to a concert; you buy a ticket; she changes her mind and decides not to go after all. No refunds.
– You park for three minutes in 10-minute parking to hand in an assignment; five minutes down the road you realize that part of the assignment is still in the car, so you turn around and park again to hand in the rest. When you come back three minutes later you have a parking ticket.
– You grab the last tag for a hot item at a Boxing Day doorbuster sale, but the cashier says it’s actually out of stock (has actually happened to me)
15
– You save a minor edit to your project report, not realizing that your project partner saved major edits since you last opened the file.
– You buy a new textbook from the bookstore because you couldn’t find a used copy; meanwhile 100 cheap used copies go on sale.
– You agree to go out with some friends on Friday because your ex- girlfriend is *not* coming; meanwhile, she decides to go because she thinks you won’t be there.
– End of month, no money. Rent deduction hits checking, tax return hits
assume overdraft protection will kick when the rent arrives. Bank says the rent arrived first and charges a $20 overdraft fee. The timestamps in the database show the rent arrived before you made your printout.
16
– Allows phantoms
– Allows WAR anomalies
– Allows RAW and WAR anomalies
– Allows snapshot anomalies and phantoms
– Allows lost updates
– Only guarantee: values read were written at some point in the past – Just about anything else can happen (WAW, lost updates, skew...)
17