--- /dev/null
+.. change::
+ :tags: bug, mssql, py3k
+ :tickets: 4273
+
+ Fixed issue within the SQL Server dialect under Python 3 where when running
+ against a non-standard SQL server database that does not contain either the
+ "sys.dm_exec_sessions" or "sys.dm_pdw_nodes_exec_sessions" views, leading
+ to a failure to fetch the isolation level, the error raise would fail due
+ to an UnboundLocalError.
+
+
raise NotImplementedError(
"Can't fetch isolation level prior to SQL Server 2005")
+ last_error = None
+
views = ("sys.dm_exec_sessions", "sys.dm_pdw_nodes_exec_sessions")
for view in views:
cursor = connection.cursor()
""" % view)
val = cursor.fetchone()[0]
except self.dbapi.Error as err:
+ # Python3 scoping rules
+ last_error = err
continue
else:
return val.upper()
finally:
cursor.close()
+ else:
+ util.warn(
+ "Could not fetch transaction isolation level, "
+ "tried views: %s; final error was: %s" % (views, last_error))
- util.warn(
- "Could not fetch transaction isolation level, "
- "tried views: %s; final error was: %s" % (views, err))
- raise NotImplementedError(
- "Can't fetch isolation level on this particular "
- "SQL Server version"
- )
+ raise NotImplementedError(
+ "Can't fetch isolation level on this particular "
+ "SQL Server version"
+ )
def initialize(self, connection):
super(MSDialect, self).initialize(connection)
from sqlalchemy.engine import url
from sqlalchemy.testing import fixtures
from sqlalchemy import testing
-from sqlalchemy.testing import assert_raises_message, assert_warnings
+from sqlalchemy.testing import assert_raises_message, \
+ assert_warnings, expect_warnings
from sqlalchemy.testing.mock import Mock
+from sqlalchemy.dialects.mssql import base
class ParseConnectTest(fixtures.TestBase):
eq_(
dialect._get_server_version_info(conn),
expected
- )
\ No newline at end of file
+ )
+
+
+class IsolationLevelDetectTest(fixtures.TestBase):
+
+ def _fixture(self, view):
+ class Error(Exception):
+ pass
+
+ dialect = pyodbc.MSDialect_pyodbc()
+ dialect.dbapi = Mock(Error=Error)
+ dialect.server_version_info = base.MS_2012_VERSION
+
+ result = []
+
+ def fail_on_exec(stmt, ):
+ if view is not None and view in stmt:
+ result.append(('SERIALIZABLE', ))
+ else:
+ raise Error("that didn't work")
+
+ connection = Mock(
+ cursor=Mock(
+ return_value=Mock(
+ execute=fail_on_exec,
+ fetchone=lambda: result[0]
+ ),
+ )
+ )
+
+ return dialect, connection
+
+ def test_dm_pdw_nodes(self):
+ dialect, connection = self._fixture("dm_pdw_nodes_exec_sessions")
+
+ eq_(
+ dialect.get_isolation_level(connection),
+ "SERIALIZABLE"
+ )
+
+ def test_exec_sessions(self):
+ dialect, connection = self._fixture("exec_sessions")
+
+ eq_(
+ dialect.get_isolation_level(connection),
+ "SERIALIZABLE"
+ )
+
+ def test_not_supported(self):
+ dialect, connection = self._fixture(None)
+
+ with expect_warnings("Could not fetch transaction isolation level"):
+ assert_raises_message(
+ NotImplementedError,
+ "Can't fetch isolation",
+ dialect.get_isolation_level, connection
+ )
+