Skip to content

Commit 95ae115

Browse files
committed
Merge pull request #32 from methane/autocommit
More precise get_autocommit based on server_status.
2 parents 9b0b59f + 2204283 commit 95ae115

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

MySQLdb/connections.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,28 +232,15 @@ def string_decoder(s):
232232
self.encoders[types.StringType] = string_literal
233233
self.encoders[types.UnicodeType] = unicode_literal
234234
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
235-
self._autocommit = None
236235
if self._transactional:
237236
if autocommit is not None:
238237
self.autocommit(autocommit)
239238
self.messages = []
240239

241240
def autocommit(self, on):
242241
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()
242+
if self.get_autocommit() != on:
243+
_mysql.connection.autocommit(self, on)
257244

258245
def cursor(self, cursorclass=None):
259246
"""

_mysql.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,21 @@ _mysql_ConnectionObject_autocommit(
891891
if (err) return _mysql_Exception(self);
892892
Py_INCREF(Py_None);
893893
return Py_None;
894-
}
894+
}
895+
896+
static char _mysql_ConnectionObject_get_autocommit__doc__[] =
897+
"Get the autocommit mode. True when enable; False when disable.\n";
898+
899+
static PyObject *
900+
_mysql_ConnectionObject_get_autocommit(
901+
_mysql_ConnectionObject *self,
902+
PyObject *args)
903+
{
904+
if (self->connection.server_status & SERVER_STATUS_AUTOCOMMIT) {
905+
Py_RETURN_TRUE;
906+
}
907+
Py_RETURN_FALSE;
908+
}
895909

896910
static char _mysql_ConnectionObject_commit__doc__[] =
897911
"Commits the current transaction\n\
@@ -2317,6 +2331,12 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = {
23172331
METH_VARARGS,
23182332
_mysql_ConnectionObject_autocommit__doc__
23192333
},
2334+
{
2335+
"get_autocommit",
2336+
(PyCFunction)_mysql_ConnectionObject_get_autocommit,
2337+
METH_NOARGS,
2338+
_mysql_ConnectionObject_get_autocommit__doc__
2339+
},
23202340
{
23212341
"commit",
23222342
(PyCFunction)_mysql_ConnectionObject_commit,

0 commit comments

Comments
 (0)