| 1 |
"""Isolation Level definitions for Dejavu |
|---|
| 2 |
|
|---|
| 3 |
We follow the terminology of ANSI for specifying isolation levels, |
|---|
| 4 |
but you should be aware that: |
|---|
| 5 |
|
|---|
| 6 |
1. The four isolation levels defined therein do not begin to cover |
|---|
| 7 |
all the differences between competing transaction schemes, and |
|---|
| 8 |
|
|---|
| 9 |
2. Different products and discussions use terms differently. |
|---|
| 10 |
|
|---|
| 11 |
In particular, http://citeseer.ist.psu.edu/berenson95critique.html |
|---|
| 12 |
shows that the ANSI definitions are themselves subject to a range |
|---|
| 13 |
of interpretations, so that even "sticking to the standard" is |
|---|
| 14 |
open to misunderstanding. For safety reasons, we use the "broad" |
|---|
| 15 |
interpretations (as defined by berenson95), so that each isolation |
|---|
| 16 |
level not only prevents the corresponding phenomena, but prevents |
|---|
| 17 |
situations which MIGHT lead to the phenomena. For example, when |
|---|
| 18 |
we say that the "Read Committed" level prevents the "Dirty Read" |
|---|
| 19 |
phenomenon, we mean that we forbid both the "strict" interpretation: |
|---|
| 20 |
|
|---|
| 21 |
Transaction 1 Transaction 2 |
|---|
| 22 |
write x |
|---|
| 23 |
read x |
|---|
| 24 |
abort commit |
|---|
| 25 |
|
|---|
| 26 |
and ALSO the "broad" interpretation: |
|---|
| 27 |
|
|---|
| 28 |
Transaction 1 Transaction 2 |
|---|
| 29 |
write x |
|---|
| 30 |
read x |
|---|
| 31 |
commit/abort commit/abort |
|---|
| 32 |
|
|---|
| 33 |
Now, "forbid" is unfortunately also open to interpretation. Different |
|---|
| 34 |
stores (databases) will exhibit different behavior for different |
|---|
| 35 |
phenomena. For the above example, PostgreSQL prohibits dirty reads |
|---|
| 36 |
(for the appropriate levels) by returning the unaltered data to |
|---|
| 37 |
Transaction 2. MySQL returns unaltered data for all levels except |
|---|
| 38 |
SERIALIZABLE, for which it raises a lock timeout. Microsoft SQL Server |
|---|
| 39 |
raises a CommandTimeout for READ COMMITTED and above. |
|---|
| 40 |
|
|---|
| 41 |
Note also that MVCC architectures parallel single-version locking |
|---|
| 42 |
schemes in ways that are difficult to equate and map perfectly. |
|---|
| 43 |
Postgres, for example, allows you to declare all four ANSI levels, |
|---|
| 44 |
but internally only uses two of them due to its MVCC architecture. |
|---|
| 45 |
|
|---|
| 46 |
Finally, note that each Storage Manager allows you to directly |
|---|
| 47 |
set the values for the isolation levels which are sent to your |
|---|
| 48 |
back end (usually via a separate configuration option). |
|---|
| 49 |
These names are merely a convenience feature to provide a |
|---|
| 50 |
common map, so that mixing and migration of stores is easier. |
|---|
| 51 |
""" |
|---|
| 52 |
|
|---|
| 53 |
anomalies = [ |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
"Dirty Write", |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
"Dirty Read", |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
"Lost Update", |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
"Cursor Lost Update", |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
"Nonrepeatable Read", |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
"Read Skew", |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
"Write Skew", |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
"Phantom", |
|---|
| 110 |
] |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
class IsolationLevel(object): |
|---|
| 114 |
|
|---|
| 115 |
def __init__(self, name, forbidden): |
|---|
| 116 |
self.name = name |
|---|
| 117 |
self.forbidden = forbidden |
|---|
| 118 |
|
|---|
| 119 |
def forbids(self, anomaly): |
|---|
| 120 |
return (anomaly in self.forbidden) |
|---|
| 121 |
|
|---|
| 122 |
def __repr__(self): |
|---|
| 123 |
return "IsolationLevel(%r)" % self.name |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
DEGREE_0 = IsolationLevel("DEGREE 0", []) |
|---|
| 127 |
READ_UNCOMMITTED = IsolationLevel("READ UNCOMMITTED", |
|---|
| 128 |
["Dirty Write"]) |
|---|
| 129 |
READ_COMMITTED = IsolationLevel("READ COMMITTED", |
|---|
| 130 |
["Dirty Write", "Dirty Read"]) |
|---|
| 131 |
CURSOR_STABILITY = IsolationLevel("CURSOR STABILITY", |
|---|
| 132 |
["Dirty Write", "Dirty Read", |
|---|
| 133 |
"Cursor Lost Update"]) |
|---|
| 134 |
SNAPSHOT = IsolationLevel("SNAPSHOT", |
|---|
| 135 |
["Dirty Write", "Dirty Read", |
|---|
| 136 |
"Cursor Lost Update", "Lost Update", |
|---|
| 137 |
"Nonrepeatable Read", "Read Skew"]) |
|---|
| 138 |
REPEATABLE_READ = IsolationLevel("REPEATABLE READ", |
|---|
| 139 |
["Dirty Write", "Dirty Read", |
|---|
| 140 |
"Cursor Lost Update", "Lost Update", |
|---|
| 141 |
"Nonrepeatable Read", "Read Skew", "Write Skew"]) |
|---|
| 142 |
SERIALIZABLE = IsolationLevel("SERIALIZABLE", |
|---|
| 143 |
["Dirty Write", "Dirty Read", |
|---|
| 144 |
"Cursor Lost Update", "Lost Update", |
|---|
| 145 |
"Nonrepeatable Read", "Phantom", |
|---|
| 146 |
"Read Skew", "Write Skew"]) |
|---|
| 147 |
|
|---|
| 148 |
levels = [DEGREE_0, READ_UNCOMMITTED, READ_COMMITTED, CURSOR_STABILITY, |
|---|
| 149 |
SNAPSHOT, REPEATABLE_READ, SERIALIZABLE] |
|---|