Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

root/trunk/geniusql/isolation.py

Revision 1 (checked in by fumanchu, 6 years ago)

Initial import.

  • Property svn:eol-style set to native
Line 
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     #       Transaction 1       Transaction 2
55     #          write x
56     #                              write x
57     #        commit/abort       commit/abort
58     "Dirty Write",
59    
60     #       Transaction 1       Transaction 2
61     #          write x
62     #                              read x
63     #        commit/abort       commit/abort
64     "Dirty Read",
65    
66     #       Transaction 1       Transaction 2
67     #          read x
68     #                              write x
69     #          write x
70     #           commit
71     "Lost Update",
72    
73     #       Transaction 1       Transaction 2
74     #          read x (cursor)
75     #                              write x
76     #          write x
77     #           commit
78     "Cursor Lost Update",
79    
80     #       Transaction 1       Transaction 2
81     #           read x
82     #                              write x
83     #        commit/abort       commit/abort
84     "Nonrepeatable Read",
85    
86     # Where x and y have consistency constraints:
87     #       Transaction 1       Transaction 2
88     #           read x
89     #                              write x
90     #                              write y
91     #                              commit
92     #           read y
93     #        commit/abort
94     "Read Skew",
95    
96     # Where x and y have consistency constraints:
97     #       Transaction 1       Transaction 2
98     #           read x
99     #                              read y
100     #           write y
101     #                              write x
102     #           commit             commit
103     "Write Skew",
104    
105     #       Transaction 1       Transaction 2
106     #           read P
107     #                           write y in P
108     #        commit/abort       commit/abort
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]
Note: See TracBrowser for help on using the browser.