From 3dbf035fa5866364a53e17b79172746fe7e9b11e Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 2 Oct 2013 02:10:02 +0900 Subject: [PATCH] More precise get_autocommit based on server_status. --- MySQLdb/connections.py | 15 ++------------- _mysql.c | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 40a6150..0c529f7 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -240,21 +240,10 @@ class Connection(_mysql.connection): def autocommit(self, on): on = bool(on) - _mysql.connection.autocommit(self, on) + if self.get_autocommit() != on: + _mysql.connection.autocommit(self, on) self._autocommit = on - def get_autocommit(self): - if self._autocommit is None: - self._update_autocommit() - return self._autocommit - - def _update_autocommit(self): - cursor = cursors.Cursor(self) - cursor.execute("SELECT @@AUTOCOMMIT") - row = cursor.fetchone() - self._autocommit = bool(row[0]) - cursor.close() - def cursor(self, cursorclass=None): """ diff --git a/_mysql.c b/_mysql.c index c0ffddb..0fd7442 100644 --- a/_mysql.c +++ b/_mysql.c @@ -891,7 +891,21 @@ _mysql_ConnectionObject_autocommit( if (err) return _mysql_Exception(self); Py_INCREF(Py_None); return Py_None; -} +} + +static char _mysql_ConnectionObject_get_autocommit__doc__[] = +"Get the autocommit mode. True when enable; False when disable.\n"; + +static PyObject * +_mysql_ConnectionObject_get_autocommit( + _mysql_ConnectionObject *self, + PyObject *args) +{ + if (self->connection.server_status & SERVER_STATUS_AUTOCOMMIT) { + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; +} static char _mysql_ConnectionObject_commit__doc__[] = "Commits the current transaction\n\ @@ -2317,6 +2331,12 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = { METH_VARARGS, _mysql_ConnectionObject_autocommit__doc__ }, + { + "get_autocommit", + (PyCFunction)_mysql_ConnectionObject_get_autocommit, + METH_NOARGS, + _mysql_ConnectionObject_get_autocommit__doc__ + }, { "commit", (PyCFunction)_mysql_ConnectionObject_commit,