Database Systems - Concurrency
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Database Systems - Concurrency

  • 2,683 views
Uploaded on

Transaction management, recovery, locking, isolation levels, intent locking.

Transaction management, recovery, locking, isolation levels, intent locking.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
2,683
On Slideshare
2,678
From Embeds
5
Number of Embeds
2

Actions

Shares
Downloads
125
Comments
3
Likes
2

Embeds 5

http://www.slideshare.net 4
http://linktostudy.blogspot.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Transactions Concurrency Database Systems Concurrency H. Turgut Uyar ¸Sule ¨O˘g¨ud¨uc¨u 2002-2014 H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 2. Transactions Concurrency License c 2002-2014 T. Uyar, S¸. O¨g˘u¨du¨cu¨ You are free to: Share – copy and redistribute the material in any medium or format Adapt – remix, transform, and build upon the material Under the following terms: Attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. NonCommercial – You may not use the material for commercial purposes. ShareAlike – If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. For more information: https://creativecommons.org/licenses/by-nc-sa/4.0/ Read the full license: https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 3. Transactions Concurrency Topics 1 Transactions Introduction Recovery Two-Phase Commit 2 Concurrency Introduction Locking Isolation Levels Intent Locks H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 4. Transactions Concurrency Introduction Recovery Two-Phase Commit References Topics 1 Transactions Introduction Recovery Two-Phase Commit 2 Concurrency Introduction Locking Isolation Levels Intent Locks H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 5. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transactions a group of operations may have to be carried out together finishing some operations while failing on others might cause inconsistency transaction: a logical unit of work no guarantee that multiple operations will all be finished at least return to the state before the changes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 6. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transactions a group of operations may have to be carried out together finishing some operations while failing on others might cause inconsistency transaction: a logical unit of work no guarantee that multiple operations will all be finished at least return to the state before the changes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 7. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transaction Example transfer money from one bank account to another UPDATE ACCOUNTS SET BALANCE = BALANCE - 100 WHERE ACCOUNTID = 123 UPDATE ACCOUNTS SET BALANCE = BALANCE + 100 WHERE ACCOUNTID = 456 H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 8. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transaction Properties Atomicity: all or nothing Consistency: move from one consistent state to another Isolation: whether operations of an unfinished transaction affect other transactions or not Durability: when a transaction is finished, its changes are permanent even if there is a system failure H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 9. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transaction Properties Atomicity: all or nothing Consistency: move from one consistent state to another Isolation: whether operations of an unfinished transaction affect other transactions or not Durability: when a transaction is finished, its changes are permanent even if there is a system failure H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 10. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transaction Properties Atomicity: all or nothing Consistency: move from one consistent state to another Isolation: whether operations of an unfinished transaction affect other transactions or not Durability: when a transaction is finished, its changes are permanent even if there is a system failure H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 11. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transaction Properties Atomicity: all or nothing Consistency: move from one consistent state to another Isolation: whether operations of an unfinished transaction affect other transactions or not Durability: when a transaction is finished, its changes are permanent even if there is a system failure H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 12. Transactions Concurrency Introduction Recovery Two-Phase Commit References SQL Transactions starting a transaction BEGIN [ WORK | TRANSACTION ] finishing a transaction COMMIT [ WORK | TRANSACTION ] cancelling a transaction ROLLBACK [ WORK | TRANSACTION ] H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 13. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transaction Example BEGIN TRANSACTION ON ERROR GOTO UNDO UPDATE ACCOUNTS SET BALANCE = BALANCE - 100 WHERE (ACCOUNTID = 123) UPDATE ACCOUNTS SET BALANCE = BALANCE + 100 WHERE (ACCOUNTID = 456) COMMIT ... UNDO: ROLLBACK H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 14. Transactions Concurrency Introduction Recovery Two-Phase Commit References Topics 1 Transactions Introduction Recovery Two-Phase Commit 2 Concurrency Introduction Locking Isolation Levels Intent Locks H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 15. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery consider a system failure during a transaction buffer cache has not been flushed to the disk how to guarantee durability? derive the data from other sources in the system H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 16. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery consider a system failure during a transaction buffer cache has not been flushed to the disk how to guarantee durability? derive the data from other sources in the system H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 17. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transaction Log log: values of every affected tuple before and after every operation write-ahead log rule: log must be flushed before the transaction is committed accessing records in the log is sequential by nature H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 18. Transactions Concurrency Introduction Recovery Two-Phase Commit References Transaction Log log: values of every affected tuple before and after every operation write-ahead log rule: log must be flushed before the transaction is committed accessing records in the log is sequential by nature H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 19. Transactions Concurrency Introduction Recovery Two-Phase Commit References Checkpoints create checkpoints at certain intervals: flush buffer cache to the physical medium note the checkpoint note the continuing transactions H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 20. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery Lists after the failure: which transactions will be undone, which transactions will be made permanent? create two lists: undo (U), redo (R) tC : last checkpoint in the log add the transactions which are active at tC to U scan records from tC to end of log add any starting transaction to U move any finishing transaction to R H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 21. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery Lists after the failure: which transactions will be undone, which transactions will be made permanent? create two lists: undo (U), redo (R) tC : last checkpoint in the log add the transactions which are active at tC to U scan records from tC to end of log add any starting transaction to U move any finishing transaction to R H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 22. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery Example tC : U = [T2,T3] R = [] T4 started: U = [T2,T3,T4] R = [] T2 finished: U = [T3,T4] R = [T2] T5 started: U = [T3,T4,T5] R = [T2] T4 finished: U = [T3,T5] R = [T2,T4] H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 23. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery Example tC : U = [T2,T3] R = [] T4 started: U = [T2,T3,T4] R = [] T2 finished: U = [T3,T4] R = [T2] T5 started: U = [T3,T4,T5] R = [T2] T4 finished: U = [T3,T5] R = [T2,T4] H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 24. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery Example tC : U = [T2,T3] R = [] T4 started: U = [T2,T3,T4] R = [] T2 finished: U = [T3,T4] R = [T2] T5 started: U = [T3,T4,T5] R = [T2] T4 finished: U = [T3,T5] R = [T2,T4] H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 25. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery Example tC : U = [T2,T3] R = [] T4 started: U = [T2,T3,T4] R = [] T2 finished: U = [T3,T4] R = [T2] T5 started: U = [T3,T4,T5] R = [T2] T4 finished: U = [T3,T5] R = [T2,T4] H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 26. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery Example tC : U = [T2,T3] R = [] T4 started: U = [T2,T3,T4] R = [] T2 finished: U = [T3,T4] R = [T2] T5 started: U = [T3,T4,T5] R = [T2] T4 finished: U = [T3,T5] R = [T2,T4] H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 27. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery Example tC : U = [T2,T3] R = [] T4 started: U = [T2,T3,T4] R = [] T2 finished: U = [T3,T4] R = [T2] T5 started: U = [T3,T4,T5] R = [T2] T4 finished: U = [T3,T5] R = [T2,T4] H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 28. Transactions Concurrency Introduction Recovery Two-Phase Commit References Recovery Process scan records backwards from end of log undo the changes made by the transactions in U scan records forwards redo the changes made by the transactions in R H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 29. Transactions Concurrency Introduction Recovery Two-Phase Commit References Topics 1 Transactions Introduction Recovery Two-Phase Commit 2 Concurrency Introduction Locking Isolation Levels Intent Locks H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 30. Transactions Concurrency Introduction Recovery Two-Phase Commit References Two-Phase Commit different source managers different undo / redo mechanisms modifications on data on different source managers either commit in all or rollback in all coordinator H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 31. Transactions Concurrency Introduction Recovery Two-Phase Commit References Protocol coordinator ! participants: flush all data regarding the transaction coordinator ! participants: start transaction and report back the result all participants succeeded: success otherwise: failure if success, coordinator ! participants: commit if failure, coordinator ! participants: rollback H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 32. Transactions Concurrency Introduction Recovery Two-Phase Commit References Protocol coordinator ! participants: flush all data regarding the transaction coordinator ! participants: start transaction and report back the result all participants succeeded: success otherwise: failure if success, coordinator ! participants: commit if failure, coordinator ! participants: rollback H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 33. Transactions Concurrency Introduction Recovery Two-Phase Commit References Protocol coordinator ! participants: flush all data regarding the transaction coordinator ! participants: start transaction and report back the result all participants succeeded: success otherwise: failure if success, coordinator ! participants: commit if failure, coordinator ! participants: rollback H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 34. Transactions Concurrency Introduction Recovery Two-Phase Commit References References Required Reading: Date Chapter 15: Recovery H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 35. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Topics 1 Transactions Introduction Recovery Two-Phase Commit 2 Concurrency Introduction Locking Isolation Levels Intent Locks H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 36. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Concurrency problems that might arise due to simultaneuous transactions: lost update uncommitted dependency inconsistent analysis H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 37. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p ... ... ... ... RETRIEVE p ... ... UPDATE p ... ... ... ... UPDATE p ... ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 38. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p ... ... ... ... RETRIEVE p ... ... UPDATE p ... ... ... ... UPDATE p ... ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 39. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p ... ... ... ... RETRIEVE p ... ... UPDATE p ... ... ... ... UPDATE p ... ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 40. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p ... ... ... ... RETRIEVE p ... ... UPDATE p ... ... ... ... UPDATE p ... ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 41. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p ... ... ... ... RETRIEVE p ... ... UPDATE p ... ... ... ... UPDATE p ... ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 42. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Uncommitted Dependency Transaction A Transaction B ... ... ... UPDATE p ... ... RETRIEVE p ... ... ... ... ROLLBACK ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 43. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Uncommitted Dependency Transaction A Transaction B ... ... ... UPDATE p ... ... RETRIEVE p ... ... ... ... ROLLBACK ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 44. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Uncommitted Dependency Transaction A Transaction B ... ... ... UPDATE p ... ... RETRIEVE p ... ... ... ... ROLLBACK ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 45. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Uncommitted Dependency Transaction A Transaction B ... ... ... UPDATE p ... ... RETRIEVE p ... ... ... ... ROLLBACK ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 46. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis compute sum of accounts: acc1=40, acc2=50, acc3=30 Transaction A Transaction B ... ... RETRIEVE acc1 (40) ... RETRIEVE acc2 (90) ... ... ... ... UPDATE acc3 (30 ! 20) ... UPDATE acc1 (40 ! 50) ... COMMIT ... ... RETRIEVE acc3 (110) ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 47. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis compute sum of accounts: acc1=40, acc2=50, acc3=30 Transaction A Transaction B ... ... RETRIEVE acc1 (40) ... RETRIEVE acc2 (90) ... ... ... ... UPDATE acc3 (30 ! 20) ... UPDATE acc1 (40 ! 50) ... COMMIT ... ... RETRIEVE acc3 (110) ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 48. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis compute sum of accounts: acc1=40, acc2=50, acc3=30 Transaction A Transaction B ... ... RETRIEVE acc1 (40) ... RETRIEVE acc2 (90) ... ... ... ... UPDATE acc3 (30 ! 20) ... UPDATE acc1 (40 ! 50) ... COMMIT ... ... RETRIEVE acc3 (110) ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 49. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis compute sum of accounts: acc1=40, acc2=50, acc3=30 Transaction A Transaction B ... ... RETRIEVE acc1 (40) ... RETRIEVE acc2 (90) ... ... ... ... UPDATE acc3 (30 ! 20) ... UPDATE acc1 (40 ! 50) ... COMMIT ... ... RETRIEVE acc3 (110) ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 50. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis compute sum of accounts: acc1=40, acc2=50, acc3=30 Transaction A Transaction B ... ... RETRIEVE acc1 (40) ... RETRIEVE acc2 (90) ... ... ... ... UPDATE acc3 (30 ! 20) ... UPDATE acc1 (40 ! 50) ... COMMIT ... ... RETRIEVE acc3 (110) ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 51. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Conflicts A reads, B reads: no problem A reads, B writes: non-repeatable read (inconsistent analysis) A writes, B reads: dirty read (uncommitted dependency) A writes, B writes: dirty write (lost update) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 52. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Topics 1 Transactions Introduction Recovery Two-Phase Commit 2 Concurrency Introduction Locking Isolation Levels Intent Locks H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 53. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Locking let transactions lock the tuples they work on shared lock (S) exclusive lock (X) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 54. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lock Requests lock type compatibility matrix S X - Y Y S Y N X N N existing shared lock: only shared lock requests are allowed existing exclusive lock: all lock requests are denied H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 55. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Locking Protocol transaction requests lock depending on operation promote a shared lock to an exclusive lock if request denied, it starts waiting it continues when the transaction that holds the lock releases it starvation H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 56. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... ... RETRIEVE p (S+) ... ... UPDATE p (X-) ... wait ... wait UPDATE p (X-) wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 57. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... ... RETRIEVE p (S+) ... ... UPDATE p (X-) ... wait ... wait UPDATE p (X-) wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 58. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... ... RETRIEVE p (S+) ... ... UPDATE p (X-) ... wait ... wait UPDATE p (X-) wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 59. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... ... RETRIEVE p (S+) ... ... UPDATE p (X-) ... wait ... wait UPDATE p (X-) wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 60. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lost Update Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... ... RETRIEVE p (S+) ... ... UPDATE p (X-) ... wait ... wait UPDATE p (X-) wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 61. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Uncommitted Dependency Transaction A Transaction B ... ... ... UPDATE p (X+) ... ... RETRIEVE p (S-) ... wait ... wait ROLLBACK RETRIEVE p (S+) ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 62. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Uncommitted Dependency Transaction A Transaction B ... ... ... UPDATE p (X+) ... ... RETRIEVE p (S-) ... wait ... wait ROLLBACK RETRIEVE p (S+) ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 63. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Uncommitted Dependency Transaction A Transaction B ... ... ... UPDATE p (X+) ... ... RETRIEVE p (S-) ... wait ... wait ROLLBACK RETRIEVE p (S+) ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 64. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Uncommitted Dependency Transaction A Transaction B ... ... ... UPDATE p (X+) ... ... RETRIEVE p (S-) ... wait ... wait ROLLBACK RETRIEVE p (S+) ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 65. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis Transaction A Transaction B ... ... RETRIEVE acc1 (S+) ... RETRIEVE acc2 (S+) ... ... ... ... UPDATE acc3 (X+) ... UPDATE acc1 (X-) ... wait RETRIEVE acc3 (S-) wait wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 66. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis Transaction A Transaction B ... ... RETRIEVE acc1 (S+) ... RETRIEVE acc2 (S+) ... ... ... ... UPDATE acc3 (X+) ... UPDATE acc1 (X-) ... wait RETRIEVE acc3 (S-) wait wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 67. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis Transaction A Transaction B ... ... RETRIEVE acc1 (S+) ... RETRIEVE acc2 (S+) ... ... ... ... UPDATE acc3 (X+) ... UPDATE acc1 (X-) ... wait RETRIEVE acc3 (S-) wait wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 68. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis Transaction A Transaction B ... ... RETRIEVE acc1 (S+) ... RETRIEVE acc2 (S+) ... ... ... ... UPDATE acc3 (X+) ... UPDATE acc1 (X-) ... wait RETRIEVE acc3 (S-) wait wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 69. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis Transaction A Transaction B ... ... RETRIEVE acc1 (S+) ... RETRIEVE acc2 (S+) ... ... ... ... UPDATE acc3 (X+) ... UPDATE acc1 (X-) ... wait RETRIEVE acc3 (S-) wait wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 70. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Inconsistent Analysis Transaction A Transaction B ... ... RETRIEVE acc1 (S+) ... RETRIEVE acc2 (S+) ... ... ... ... UPDATE acc3 (X+) ... UPDATE acc1 (X-) ... wait RETRIEVE acc3 (S-) wait wait wait H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 71. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Deadlock deadlock: transactions waiting for each other to release the locks almost always between two transactions detect and solve prevent H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 72. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Solving Deadlocks Example wait graph choose a victim and kill it H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 73. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Preventing Deadlocks every transaction has a starting timestamp if A’s request conflicts with B’s lock: wait-die: A waits if older than B, otherwise it dies A is rolled back and restarted wound-wait: A waits if younger than B, otherwise it wounds B B is rolled back and restarted timestamp of restarted transaction is not changed H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 74. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Preventing Deadlocks every transaction has a starting timestamp if A’s request conflicts with B’s lock: wait-die: A waits if older than B, otherwise it dies A is rolled back and restarted wound-wait: A waits if younger than B, otherwise it wounds B B is rolled back and restarted timestamp of restarted transaction is not changed H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 75. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Preventing Deadlocks every transaction has a starting timestamp if A’s request conflicts with B’s lock: wait-die: A waits if older than B, otherwise it dies A is rolled back and restarted wound-wait: A waits if younger than B, otherwise it wounds B B is rolled back and restarted timestamp of restarted transaction is not changed H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 76. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lock Statements shared lock SELECT query FOR SHARE exclusive lock SELECT query FOR UPDATE H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 77. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Topics 1 Transactions Introduction Recovery Two-Phase Commit 2 Concurrency Introduction Locking Isolation Levels Intent Locks H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 78. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Isolation Levels if isolation is decreased, concurrency can be increased various isolation levels serializable repeatable read read committed read uncommitted H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 79. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Serializability serial execution: a transaction starts only after another is finished serializable: result of concurrent execution is always the same as one of the serial executions Example x = 10 transaction A: x = x + 1 transaction B: x = 2  x first A, then B: x = 22 first B, then A: x = 21 H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 80. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Serializability serial execution: a transaction starts only after another is finished serializable: result of concurrent execution is always the same as one of the serial executions Example x = 10 transaction A: x = x + 1 transaction B: x = 2  x first A, then B: x = 22 first B, then A: x = 21 H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 81. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Serializability serial execution: a transaction starts only after another is finished serializable: result of concurrent execution is always the same as one of the serial executions Example x = 10 transaction A: x = x + 1 transaction B: x = 2  x first A, then B: x = 22 first B, then A: x = 21 H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 82. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Serializability serial execution: a transaction starts only after another is finished serializable: result of concurrent execution is always the same as one of the serial executions Example x = 10 transaction A: x = x + 1 transaction B: x = 2  x first A, then B: x = 22 first B, then A: x = 21 H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 83. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Two-Phase Locking Protocol two-phase locking: after any lock is released, no more new lock requests expansion phase: gather locks contraction phase: release locks two-phase strict locking: all locks are released at the end of the transaction If all transactions obey the two-phase locking protocol, all concurrent executions are serializable. H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 84. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Two-Phase Locking Protocol two-phase locking: after any lock is released, no more new lock requests expansion phase: gather locks contraction phase: release locks two-phase strict locking: all locks are released at the end of the transaction If all transactions obey the two-phase locking protocol, all concurrent executions are serializable. H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 85. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Read Committed read committed: only X locks are held until end of transaction Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... release lock ... ... ... ... UPDATE p (X+) ... COMMIT RETRIEVE p (S+) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 86. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Read Committed read committed: only X locks are held until end of transaction Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... release lock ... ... ... ... UPDATE p (X+) ... COMMIT RETRIEVE p (S+) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 87. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Read Committed read committed: only X locks are held until end of transaction Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... release lock ... ... ... ... UPDATE p (X+) ... COMMIT RETRIEVE p (S+) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 88. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Read Committed read committed: only X locks are held until end of transaction Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... release lock ... ... ... ... UPDATE p (X+) ... COMMIT RETRIEVE p (S+) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 89. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Read Committed read committed: only X locks are held until end of transaction Transaction A Transaction B ... ... RETRIEVE p (S+) ... ... ... release lock ... ... ... ... UPDATE p (X+) ... COMMIT RETRIEVE p (S+) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 90. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Phantoms phantom: when the query is executed again, new tuples appear Example A computes the average of a customer’s account balances: 100+100+100 3 = 100 B creates new account (200) for the same customer A computes again: 100+100+100+200 4 = 125 H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 91. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Phantoms phantom: when the query is executed again, new tuples appear Example A computes the average of a customer’s account balances: 100+100+100 3 = 100 B creates new account (200) for the same customer A computes again: 100+100+100+200 4 = 125 H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 92. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Isolation Levels setting an isolation level SET TRANSACTION ISOLATION LEVEL [ SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED ] H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 93. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Isolation Level Problems dirty non-repeatable phantom read read READ UNCOMMITTED Y Y Y READ COMMITTED N Y Y REPEATABLE READ N N Y SERIALIZABLE N N N H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 94. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Topics 1 Transactions Introduction Recovery Two-Phase Commit 2 Concurrency Introduction Locking Isolation Levels Intent Locks H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 95. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Locking Granularity lock relations instead of tuples even the entire database granularity: unit of locking if granularity is increased, concurrency is decreased hard to find locks on tuples ! first, get intent locks on relation variables H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 96. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Locking Granularity lock relations instead of tuples even the entire database granularity: unit of locking if granularity is increased, concurrency is decreased hard to find locks on tuples ! first, get intent locks on relation variables H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 97. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Intent Locks Intent Shared (IS): transaction intends to read some tuples Intent Exclusive (IX): IS + transaction intends to write some tuples Shared (S): concurrent readers are allowed but no concurrent writers Shared + Intent Exclusive (SIX) Exclusive (X): no concurrency allowed on this relation H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 98. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lock Requests lock compatibility matrix IS IX S SIX X - Y Y Y Y Y IS Y Y Y Y N IX Y Y N N N S Y N Y N N SIX Y N N N N X N N N N N H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 99. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Lock Precedence for an S on a tuple, at least an IS on the relation for an X on a tuple, at least an IX on the relation H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 100. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References Locking Statements lock a table LOCK [ TABLE ] table_name [ IN lock_mode MODE ] lock modes: ACCESS SHARE ROW SHARE ROW EXCLUSIVE SHARE UPDATE EXCLUSIVE SHARE SHARE ROW EXCLUSIVE EXCLUSIVE ACCESS EXCLUSIVE H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  • 101. Transactions Concurrency Introduction Locking Isolation Levels Intent Locks References References Required Reading: Date Chapter 16: Concurrency H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems