2013 07 Transaction Isolation Level
Upcoming SlideShare
Loading in...5
×
 

2013 07 Transaction Isolation Level

on

  • 145 views

教學用,關於Isolation Level的說明。在不同的Isolation Level下會遇到什麼樣的問題等等。

教學用,關於Isolation Level的說明。在不同的Isolation Level下會遇到什麼樣的問題等等。

Statistics

Views

Total Views
145
Views on SlideShare
145
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

2013 07 Transaction Isolation Level 2013 07 Transaction Isolation Level Presentation Transcript

  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Transaction Isolation Levels of Database System Aecho Penpower, Inc aecho.liu@penpower.com.tw 2013, 07 1 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Overview Overview Anomalies and Isolation Levels Anomalies Dirty Reads Unrepeatable Reads Phantoms Isolation Levels Default level Locks of Isolation Levels FAQ 2 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ References • Database Management Systems, 3rd edition. • Isolation level, @wiki. http://goo.gl/NYSza • Isolation level, @msdn. http://goo.gl/deqhV • Acid, @wiki. https://en.wikipedia.org/wiki/ACID 3 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Brief ACID properties of database system. • Atomic Each transaction is regarded as atomic. • Consistency The consistency property ensures that any transaction will bring the database from one valid state to another. • Isolation Users should be able to understand a transaction without considering the effect of other concurrently executing transactions. • Durability Once the DBMS informs the user that a transaction has been successfully completed, its effects should persist even if the system crashes before all its changes are reflected on disk. 4 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Brief Strict 2-Phase Locking1 . Rule 1 . • Shared lock Allow another transaction to read. . • Exclusive lock No allow another transaction to read or write. . Rule 2 . All locks held by a transaction are released when the transaction is completed. . 1 Strict 2PL 5 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Brief About Isolation Level • Most DBA System offers a number of transaction isolation levels, which control the degree of locking when selecting data. • The higher isolation level, the more locks needed. It is trade-off. • For concurrency control, with multiple transactions. 6 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Brief Level/Locks Read Uncommitted Read Committed Repeatable Read Serializable Write Lock X X X Read Lock S X X Range Lock X • X → Exclusive Lock • S → Shared Lock • - → Nothing 7 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Brief Level/Anomaly Read Uncommitted Read Committed Repeatable Read Serializable Dirty Read Maybe No No No Unrepetable read Maybe Maybe No No Phantom Maybe Maybe Maybe No 8 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Anomalies • Dirty Reads • Unrepeatable Reads • Phantoms 9 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Anomalies • Dirty Reads • Unrepeatable Reads • Phantoms A Sample User Table id 1 2 name Joe Jill age 20 25 10 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Dirty Reads . When a transaction is allowed to read data from a row that has been modified by another running transaction and not yet committed. . 11 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Dirty Reads Level/Anomaly Read Uncommitted Read Committed Repeatable Read Serializable Level/Locks Read Uncommitted Read Committed Repeatable Read Serializable Dirty Read Maybe No No No Unrepetable read Maybe Maybe No No Write Lock X X X Read Lock S X X Phantom Maybe Maybe Maybe No Range Lock X 12 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Dirty Reads Transaction 1 . /∗ Query 1 ∗/ SELECT ag e FROM u s e r s WHERE i d = 1; /∗ w i l l r e a d 20 ∗/ Transaction 2 . /∗ Query 2 ∗/ UPDATE u s e r s SET age = 21 WHERE i d = 1; /∗ No commit h e r e ∗/ . . . /∗ Query 1 ∗/ SELECT ag e FROM u s e r s WHERE i d = 1; /∗ w i l l r e a d 21 ∗/ . ROLLBACK ; . . 13 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Unrepeatable Reads . A row is retrieved twice and the values within the row differ between reads. . 14 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Unrepeatable Reads Level/Anomaly Read Uncommitted Read Committed Repeatable Read Serializable Level/Locks Read Uncommitted Read Committed Repeatable Read Serializable Dirty Read Maybe No No No Unrepetable read Maybe Maybe No No Write Lock X X X Read Lock S X X Phantom Maybe Maybe Maybe No Range Lock X 15 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Unrepeatable Reads Transaction 1 . /∗ Query 1 ∗/ SELECT ∗ FROM u s e r s WHERE i d = 1 ; Transaction 2 . /∗ Query 2 ∗/ UPDATE u s e r s SET age = 21 WHERE i d = 1; COMMIT; . . . /∗ Query 1 ∗/ SELECT ∗ FROM u s e r s WHERE i d = 1 ; COMMIT; . 16 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Phantoms . when two identical queries are executed, and the collection of rows returned by the second query is different from the first. . 17 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Phantoms Level/Anomaly Read Uncommitted Read Committed Repeatable Read Serializable Level/Locks Read Uncommitted Read Committed Repeatable Read Serializable Dirty Read Maybe No No No Unrepetable read Maybe Maybe No No Write Lock X X X Read Lock S X X Phantom Maybe Maybe Maybe No Range Lock X 18 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Phantoms Transaction 1 . /∗ Query 1 ∗/ SELECT ∗ FROM u s e r s WHERE age BETWEEN 10 AND 3 0 ; Transaction 2 . /∗ Query 2 ∗/ INSERT INTO u s e r s ( i d , name , a g e ) VALUES ( 3 , ’ Bob ’ , 27 ) ; COMMIT; . . . /∗ Query 1 ∗/ SELECT ∗ FROM u s e r s WHERE age BETWEEN 10 AND 3 0 ; . 19 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Isolation Levels • Read Uncommitted The lowest level. • Read Committed • Repeatable Read • Serializable The highest level. 20 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Isolation Levels The default isolation level. • Sqlite • Serializable by default.2 • Able to switch to Read uncommitted. • Mssql • Read Committed by default.3 • Serializable → WCC’s Category tree. 2 Sqlite pragma statements. http://goo.gl/pYNwN 3 Isolation level, @msdn. http://goo.gl/deqhV 21 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Isolation Levels Level/Locks Read Uncommitted Read Committed Repeatable Read Serializable Write Lock X X X Read Lock S X X Range Lock X • X → Exclusive Lock • S → Shared Lock • - → Nothing 22 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Read Committed • Exclusive locks • Obtains before writing objects. • Released until the end of transaction. 23 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Read Committed • Exclusive locks • Obtains before writing objects. • Released until the end of transaction. • Shared locks • Obtained before reading objects. • Released immediately. 24 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Repeatable Reads and Serializable • Obtains exclusive locks before reading or writing. • All locks released until the end of transaction, according to Strict 2-PL. 25 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Repeatable Reads and Serializable • Obtains exclusive locks before reading or writing. • All locks released until the end of transaction, according to Strict 2-PL. . . • Repeatable Reads → locks individual object. • Serializable → locks set of objects. 26 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Transaction and locks When the transaction waits for lock resources ... • Wait to gain the locks. • Commit or Rollback 27 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Transaction and locks When the transaction waits for lock resources ... • Wait to gain the locks. • Commit or Rollback • Dead lock... ? 28 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Transaction and locks When the transaction waits for lock resources ... • Wait to gain the locks. • Commit or Rollback • Dead lock... ? . Set up time-out to prevent dead lock. . 29 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ Thinking In practice, what isolation level do we need ? 30 / 31
  • Overview ... Anomalies .. .. .. Isolation Levels . ..... FAQ The End FAQ, any questions ? 31 / 31