Top Banner
. . . Overview . . . . . . Anomalies . . . . . . Isolation Levels FAQ Transaction Isolation Levels of Database System Aecho Penpower, Inc [email protected] 2013, 07 1 / 31
31

2013 07 Transaction Isolation Level

Jun 28, 2015

Download

Education

Hung-Wei Liu

教學用,關於Isolation Level的說明。在不同的Isolation Level下會遇到什麼樣的問題等等。
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2013 07 Transaction Isolation Level

. . .Overview

. .

. .

. .

Anomalies.. . . . .

Isolation Levels FAQ

Transaction Isolation Levels of Database System

Aecho

Penpower, Inc

[email protected]

2013, 07

1 / 31

Page 2: 2013 07 Transaction Isolation Level

. . .Overview

. .

. .

. .

Anomalies.. . . . .

Isolation Levels FAQ

Overview

OverviewAnomalies and Isolation Levels

AnomaliesDirty ReadsUnrepeatable ReadsPhantoms

Isolation LevelsDefault levelLocks of Isolation Levels

FAQ

2 / 31

Page 3: 2013 07 Transaction Isolation Level

. . .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

Page 4: 2013 07 Transaction Isolation Level

. . .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

Page 5: 2013 07 Transaction Isolation Level

. . .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

Page 6: 2013 07 Transaction Isolation Level

. . .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

Page 7: 2013 07 Transaction Isolation Level

. . .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

Page 8: 2013 07 Transaction Isolation Level

. . .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

Page 9: 2013 07 Transaction Isolation Level

. . .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

Page 10: 2013 07 Transaction Isolation Level

. . .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

Page 11: 2013 07 Transaction Isolation Level

. . .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

Page 12: 2013 07 Transaction Isolation Level

. . .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

Page 13: 2013 07 Transaction Isolation Level

. . .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

Page 14: 2013 07 Transaction Isolation Level

. . .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

Page 15: 2013 07 Transaction Isolation Level

. . .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

Page 16: 2013 07 Transaction Isolation Level

. . .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

Page 17: 2013 07 Transaction Isolation Level

. . .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

Page 18: 2013 07 Transaction Isolation Level

. . .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

Page 19: 2013 07 Transaction Isolation Level

. . .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

Page 20: 2013 07 Transaction Isolation Level

. . .Overview

. .

. .

. .

Anomalies.. . . . .

Isolation Levels FAQ

Isolation Levels

• Read UncommittedThe lowest level.

• Read Committed

• Repeatable Read

• SerializableThe highest level.

20 / 31

Page 21: 2013 07 Transaction Isolation Level

. . .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

Page 22: 2013 07 Transaction Isolation Level

. . .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

Page 23: 2013 07 Transaction Isolation Level

. . .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

Page 24: 2013 07 Transaction Isolation Level

. . .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

Page 25: 2013 07 Transaction Isolation Level

. . .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

Page 26: 2013 07 Transaction Isolation Level

. . .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

Page 27: 2013 07 Transaction Isolation Level

. . .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

Page 28: 2013 07 Transaction Isolation Level

. . .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

Page 29: 2013 07 Transaction Isolation Level

. . .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

Page 30: 2013 07 Transaction Isolation Level

. . .Overview

. .

. .

. .

Anomalies.. . . . .

Isolation Levels FAQ

Thinking

In practice, what isolation level do we need ?

30 / 31

Page 31: 2013 07 Transaction Isolation Level

. . .Overview

. .

. .

. .

Anomalies.. . . . .

Isolation Levels FAQ

The End

FAQ, any questions ?

31 / 31