Date post: | 28-Jun-2015 |
Category: |
Education |
Upload: | hung-wei-liu |
View: | 117 times |
Download: | 0 times |
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Transaction Isolation Levels of Database System
Aecho
Penpower, Inc
2013, 07
1 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Overview
OverviewAnomalies and Isolation Levels
AnomaliesDirty ReadsUnrepeatable ReadsPhantoms
Isolation LevelsDefault levelLocks 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.
• AtomicEach transaction is regarded as atomic.
• ConsistencyThe consistency property ensures that any transaction willbring the database from one valid state to another.
• IsolationUsers should be able to understand a transaction withoutconsidering the effect of other concurrently executingtransactions.
• DurabilityOnce the DBMS informs the user that a transaction has beensuccessfully completed, its effects should persist even ifthe system crashes before all its changes are reflected ondisk.
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 iscompleted.
1Strict 2PL5 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Brief
About Isolation Level
• Most DBA System offers a number of transaction isolationlevels, which control the degree of locking when selectingdata.
• 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 Write Lock Read Lock Range Lock
Read Uncommitted - - -Read Committed X S -
Repeatable Read X X -Serializable X X X
• X → Exclusive Lock
• S → Shared Lock
• - → Nothing
7 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
BriefLevel/Anomaly Dirty Read Unrepetable read Phantom
Read Uncommitted Maybe Maybe MaybeRead Committed No Maybe Maybe
Repeatable Read No No MaybeSerializable No No No
8 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Anomalies
• Dirty Reads
• Unrepeatable Reads
• Phantoms
A Sample User Table
id name age
1 Joe 202 Jill 25
9 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Anomalies
• Dirty Reads
• Unrepeatable Reads
• Phantoms
A Sample User Table
id name age
1 Joe 202 Jill 25
10 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Dirty Reads.
......
When a transaction is allowed to read data from a row that hasbeen modified by another running transaction and not yetcommitted.
Level/Anomaly Dirty Read Unrepetable read Phantom
Read Uncommitted Maybe Maybe MaybeRead Committed No Maybe Maybe
Repeatable Read No No MaybeSerializable No No No
Level/Locks Write Lock Read Lock Range Lock
Read Uncommitted - - -Read Committed X S -
Repeatable Read X X -Serializable X X X
11 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Dirty Reads
Level/Anomaly Dirty Read Unrepetable read Phantom
Read Uncommitted Maybe Maybe MaybeRead Committed No Maybe Maybe
Repeatable Read No No MaybeSerializable No No No
Level/Locks Write Lock Read Lock Range Lock
Read Uncommitted - - -Read Committed X S -
Repeatable Read X X -Serializable X X X
12 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Dirty Reads
Transaction 1.
......
/∗ Query 1 ∗/SELECT age FROM u s e r s WHERE i d =
1 ;/∗ w i l l r ead 20 ∗/
.
......
/∗ Query 1 ∗/SELECT age FROM u s e r s WHERE i d =
1 ;/∗ w i l l r ead 21 ∗/
Transaction 2
.
......
/∗ Query 2 ∗/UPDATE u s e r s SET age = 21 WHERE i d
= 1 ;/∗ No commit he r e ∗/
.
......
ROLLBACK;
13 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Unrepeatable Reads
.
......
A row is retrieved twice and the values within the row differbetween reads.
Level/Anomaly Dirty Read Unrepetable read Phantom
Read Uncommitted Maybe Maybe MaybeRead Committed No Maybe Maybe
Repeatable Read No No MaybeSerializable No No No
Level/Locks Write Lock Read Lock Range Lock
Read Uncommitted - - -Read Committed X S -
Repeatable Read X X -Serializable X X X
14 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Unrepeatable Reads
Level/Anomaly Dirty Read Unrepetable read Phantom
Read Uncommitted Maybe Maybe MaybeRead Committed No Maybe Maybe
Repeatable Read No No MaybeSerializable No No No
Level/Locks Write Lock Read Lock Range Lock
Read Uncommitted - - -Read Committed X S -
Repeatable Read X X -Serializable X X 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 ;
.
......
/∗ Query 1 ∗/SELECT ∗ FROM u s e r s WHERE i d = 1 ;COMMIT;
Transaction 2
.
......
/∗ Query 2 ∗/UPDATE u s e r s SET age = 21 WHERE i d
= 1 ;COMMIT;
16 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Phantoms
.
......
when two identical queries are executed, and the collection of rowsreturned by the second query is different from the first.
Level/Anomaly Dirty Read Unrepetable read Phantom
Read Uncommitted Maybe Maybe MaybeRead Committed No Maybe MaybeRepeatable Read No No Maybe
Serializable No No No
Level/Locks Write Lock Read Lock Range Lock
Read Uncommitted - - -Read Committed X S -
Repeatable Read X X -Serializable X X X
17 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Phantoms
Level/Anomaly Dirty Read Unrepetable read Phantom
Read Uncommitted Maybe Maybe MaybeRead Committed No Maybe MaybeRepeatable Read No No Maybe
Serializable No No No
Level/Locks Write Lock Read Lock Range Lock
Read Uncommitted - - -Read Committed X S -
Repeatable Read X X -Serializable X X X
18 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Phantoms
Transaction 1.
......
/∗ Query 1 ∗/SELECT ∗ FROM u s e r sWHERE age BETWEEN 10 AND 30 ;
.
......
/∗ Query 1 ∗/SELECT ∗ FROM u s e r sWHERE age BETWEEN 10 AND 30 ;
Transaction 2
.
......
/∗ Query 2 ∗/INSERT INTO u s e r s ( id , name , age )
VALUES ( 3 , ’Bob ’ , 27 ) ;COMMIT;
19 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Isolation Levels
• Read UncommittedThe lowest level.
• Read Committed
• Repeatable Read
• SerializableThe 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.
2Sqlite pragma statements. http://goo.gl/pYNwN
3Isolation level, @msdn. http://goo.gl/deqhV21 / 31
. . .Overview
. .
. .
. .
Anomalies.. . . . .
Isolation Levels FAQ
Isolation Levels
Level/Locks Write Lock Read Lock Range Lock
Read Uncommitted - - -Read Committed X S -
Repeatable Read X X -Serializable X X 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.
• Shared locks
• Obtained before reading objects.• Released immediately.
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 toStrict 2-PL.
.
......
• Repeatable Reads → locks individual object.
• Serializable → locks set of objects.
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 toStrict 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
• Dead lock... ?
.
......Set up time-out to prevent dead lock.
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... ?
.
......Set up time-out to prevent 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