Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue 199: with cxn: doesn't commit on success

  • Loading branch information...
commit a6b400f39407c2ba3cd7eeeff14bafbc16d5d676 1 parent 0d47ebe
@mkleehammer authored
Showing with 24 additions and 8 deletions.
  1. +6 −1 src/connection.cpp
  2. +18 −7 tests/sqlservertests.py
View
7 src/connection.cpp
@@ -883,7 +883,12 @@ static PyObject* Connection_enter(PyObject* self)
static char exit_doc[] = "__exit__(*excinfo) -> None. Closes the connection.";
static PyObject* Connection_exit(Connection* cnxn, PyObject* args)
{
- Connection_clear(cnxn);
+ // If an error has occurred, `args` will be a tuple of 3 values. Otherwise it will be a tuple of 3 `None`s.
+ I(PyTuple_Check(args));
+
+ if (cnxn->nAutoCommit == SQL_AUTOCOMMIT_OFF && PyTuple_GetItem(args, 0) == Py_None)
+ SQLEndTran(SQL_HANDLE_DBC, cnxn->hdbc, SQL_COMMIT);
+
Py_RETURN_NONE;
}
View
25 tests/sqlservertests.py
@@ -1232,14 +1232,25 @@ def test_row_gtlt(self):
rows = list(rows)
rows.sort() # uses <
- def test_context_manager(self):
- with pyodbc.connect(self.connection_string) as cnxn:
- cnxn.getinfo(pyodbc.SQL_DEFAULT_TXN_ISOLATION)
+ def test_context_manager_success(self):
+
+ self.cursor.execute("create table t1(n int)")
+ self.cnxn.commit()
+
+ try:
+ with pyodbc.connect(self.connection_string) as cnxn:
+ cursor = cnxn.cursor()
+ cursor.execute("insert into t1 values (1)")
+ except Exception:
+ pass
+
+ cnxn = None
+ cursor = None
+
+ rows = self.cursor.execute("select n from t1").fetchall()
+ self.assertEquals(len(rows), 1)
+ self.assertEquals(rows[0][0], 1)
- # The connection should be closed now.
- def test():
- cnxn.getinfo(pyodbc.SQL_DEFAULT_TXN_ISOLATION)
- self.assertRaises(pyodbc.ProgrammingError, test)
def test_untyped_none(self):
# From issue 129
Please sign in to comment.
Something went wrong with that request. Please try again.