diff options
-rw-r--r-- | lib/sqlalchemy/engine/base.py | 15 | ||||
-rw-r--r-- | lib/sqlalchemy/engine/default.py | 11 | ||||
-rw-r--r-- | test/engine/test_transaction.py | 29 |
3 files changed, 55 insertions, 0 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 9eb1b8b40..1727e6905 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -524,6 +524,21 @@ class Dialect(object): """ return None + def reset_isolation_level(self, dbapi_conn): + """Given a DBAPI connection, revert its isolation to the default.""" + + raise NotImplementedError() + + def set_isolation_level(self, dbapi_conn, level): + """Given a DBAPI connection, set its isolation level.""" + + raise NotImplementedError() + + def get_isolation_level(self, dbapi_conn): + """Given a DBAPI connection, return its isolation level.""" + + raise NotImplementedError() + class ExecutionContext(object): """A messenger object for a Dialect that corresponds to a single diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index da6ed12a6..30a53bb20 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -175,6 +175,12 @@ class DefaultDialect(base.Dialect): except NotImplementedError: self.default_schema_name = None + try: + self.default_isolation_level = \ + self.get_isolation_level(connection.connection) + except NotImplementedError: + self.default_isolation_level = None + self.returns_unicode_strings = self._check_unicode_returns(connection) self.do_rollback(connection.connection) @@ -320,6 +326,11 @@ class DefaultDialect(base.Dialect): def is_disconnect(self, e): return False + def reset_isolation_level(self, dbapi_conn): + self.set_isolation_level(dbapi_conn, + self.isolation_level or self.default_isolation_level) + + class DefaultExecutionContext(base.ExecutionContext): isinsert = False diff --git a/test/engine/test_transaction.py b/test/engine/test_transaction.py index 1fb0267bb..6878fea39 100644 --- a/test/engine/test_transaction.py +++ b/test/engine/test_transaction.py @@ -1164,6 +1164,35 @@ class IsolationLevelTest(TestBase): eq_(isolation_level, self._default_isolation_level()) @testing.requires.isolation_level + def test_reset_level(self): + eng = create_engine(testing.db.url) + conn = eng.connect() + eq_(eng.dialect.get_isolation_level(conn.connection), self._default_isolation_level()) + + eng.dialect.set_isolation_level(conn.connection, self._non_default_isolation_level()) + eq_(eng.dialect.get_isolation_level(conn.connection), self._non_default_isolation_level()) + + eng.dialect.reset_isolation_level(conn.connection) + eq_(eng.dialect.get_isolation_level(conn.connection), self._default_isolation_level()) + + conn.close() + + @testing.requires.isolation_level + def test_reset_level_with_setting(self): + eng = create_engine(testing.db.url, isolation_level=self._non_default_isolation_level()) + conn = eng.connect() + eq_(eng.dialect.get_isolation_level(conn.connection), self._non_default_isolation_level()) + + eng.dialect.set_isolation_level(conn.connection, self._default_isolation_level()) + eq_(eng.dialect.get_isolation_level(conn.connection), self._default_isolation_level()) + + eng.dialect.reset_isolation_level(conn.connection) + eq_(eng.dialect.get_isolation_level(conn.connection), self._non_default_isolation_level()) + + conn.close() + + + @testing.requires.isolation_level def test_invalid_level(self): eng = create_engine(testing.db.url, isolation_level='FOO') assert_raises_message( |