@@ -187,6 +187,9 @@ class object, used to create cursors (keyword only)
187
187
188
188
kwargs2 ['client_flag' ] = client_flag
189
189
190
+ # PEP-249 requires autocommit to be initially off
191
+ autocommit = kwargs2 .pop ('autocommit' , False )
192
+
190
193
super (Connection , self ).__init__ (* args , ** kwargs2 )
191
194
self .cursorclass = cursorclass
192
195
self .encoders = dict ([ (k , v ) for k , v in conv .items ()
@@ -229,13 +232,29 @@ def string_decoder(s):
229
232
self .encoders [types .StringType ] = string_literal
230
233
self .encoders [types .UnicodeType ] = unicode_literal
231
234
self ._transactional = self .server_capabilities & CLIENT .TRANSACTIONS
235
+ self ._autocommit = None
232
236
if self ._transactional :
233
- # PEP-249 requires autocommit to be initially off
234
- autocommit = kwargs2 .pop ('autocommit' , False )
235
237
if autocommit is not None :
236
- self .autocommit (bool ( autocommit ) )
238
+ self .autocommit (autocommit )
237
239
self .messages = []
238
240
241
+ def autocommit (self , on ):
242
+ on = bool (on )
243
+ _mysql .connection .autocommit (self , on )
244
+ self ._autocommit = on
245
+
246
+ def get_autocommit (self ):
247
+ if self ._autocommit is None :
248
+ self ._update_autocommit ()
249
+ return self ._autocommit
250
+
251
+ def _update_autocommit (self ):
252
+ cursor = cursors .Cursor (self )
253
+ cursor .execute ("SELECT @@AUTOCOMMIT" )
254
+ row = cursor .fetchone ()
255
+ self ._autocommit = bool (row [0 ])
256
+ cursor .close ()
257
+
239
258
def cursor (self , cursorclass = None ):
240
259
"""
241
260
@@ -248,6 +267,8 @@ def cursor(self, cursorclass=None):
248
267
return (cursorclass or self .cursorclass )(self )
249
268
250
269
def __enter__ (self ):
270
+ if self .get_autocommit ():
271
+ self .query ("BEGIN" )
251
272
return self .cursor ()
252
273
253
274
def __exit__ (self , exc , value , tb ):
0 commit comments