@@ -134,9 +134,19 @@ def info(self):
134
134
def connection (self ):
135
135
return self ._connection
136
136
137
- def begin (self ):
137
+ def begin (self , isolation_level = None , readonly = False , deferrable = False ):
138
138
"""Begin a transaction and return a transaction handle.
139
139
140
+ isolation_level - The isolation level of the transaction,
141
+ should be one of 'SERIALIZABLE', 'REPEATABLE READ', 'READ COMMITTED',
142
+ 'READ UNCOMMITTED', default (None) is 'READ COMMITTED'
143
+
144
+ readonly - The transaction is read only
145
+
146
+ deferrable - The transaction may block when acquiring data before
147
+ running without overhead of SERLIALIZABLE, has no effect unless
148
+ transaction is both SERIALIZABLE and readonly
149
+
140
150
The returned object is an instance of Transaction. This
141
151
object represents the "scope" of the transaction, which
142
152
completes when either the .rollback or .commit method is
@@ -161,23 +171,31 @@ def begin(self):
161
171
.begin_twophase - use a two phase/XA transaction
162
172
163
173
"""
164
- coro = self ._begin ()
174
+ coro = self ._begin (isolation_level , readonly , deferrable )
165
175
return _TransactionContextManager (coro )
166
176
167
177
@asyncio .coroutine
168
- def _begin (self ):
178
+ def _begin (self , isolation_level , readonly , deferrable ):
169
179
if self ._transaction is None :
170
180
self ._transaction = RootTransaction (self )
171
- yield from self ._begin_impl ()
181
+ yield from self ._begin_impl (isolation_level , readonly , deferrable )
172
182
return self ._transaction
173
183
else :
174
184
return Transaction (self , self ._transaction )
175
185
176
186
@asyncio .coroutine
177
- def _begin_impl (self ):
187
+ def _begin_impl (self , isolation_level , readonly , deferrable ):
188
+ stmt = 'BEGIN'
189
+ if isolation_level is not None :
190
+ stmt += ' ISOLATION LEVEL ' + isolation_level
191
+ if readonly :
192
+ stmt += ' READ ONLY'
193
+ if deferrable :
194
+ stmt += ' DEFERRABLE'
195
+
178
196
cur = yield from self ._connection .cursor ()
179
197
try :
180
- yield from cur .execute ('BEGIN' )
198
+ yield from cur .execute (stmt )
181
199
finally :
182
200
cur .close ()
183
201
@@ -217,7 +235,7 @@ def begin_nested(self):
217
235
def _begin_nested (self ):
218
236
if self ._transaction is None :
219
237
self ._transaction = RootTransaction (self )
220
- yield from self ._begin_impl ()
238
+ yield from self ._begin_impl (None , False , False )
221
239
else :
222
240
self ._transaction = NestedTransaction (self , self ._transaction )
223
241
self ._transaction ._savepoint = yield from self ._savepoint_impl ()
0 commit comments