--- /dev/null
+.. change::
+ :tags: bug, mssql
+ :tickets: 4095
+ :versions: 1.2.0b3
+
+ Added a full range of "connection closed" exception codes to the
+ PyODBC dialect for SQL Server, including '08S01', '01002', '08003',
+ '08007', '08S02', '08001', 'HYT00', 'HY010'. Previously, only '08S01'
+ was covered.
\ No newline at end of file
if isinstance(e, self.dbapi.ProgrammingError):
return "The cursor's connection has been closed." in str(e) or \
'Attempt to use a closed connection.' in str(e)
- elif isinstance(e, self.dbapi.Error):
- return '[08S01]' in str(e)
else:
return False
version.append(n)
return tuple(version)
+ def is_disconnect(self, e, connection, cursor):
+ if isinstance(e, self.dbapi.Error):
+ for code in (
+ '08S01', '01002', '08003', '08007',
+ '08S02', '08001', 'HYT00', 'HY010'):
+ if code in str(e):
+ return True
+ return super(MSDialect_pyodbc, self).is_disconnect(
+ e, connection, cursor)
+
dialect = MSDialect_pyodbc
eq_(dialect.is_disconnect("not an error", None, None), False)
+ def test_pyodbc_disconnect(self):
+ dialect = pyodbc.dialect()
+
+ class MockDBAPIError(Exception):
+ pass
+
+ class MockProgrammingError(MockDBAPIError):
+ pass
+
+ dialect.dbapi = Mock(
+ Error=MockDBAPIError, ProgrammingError=MockProgrammingError)
+
+ for error in [
+ MockDBAPIError("[%s] some pyodbc message" % code)
+ for code in [
+ '08S01', '01002', '08003', '08007',
+ '08S02', '08001', 'HYT00', 'HY010']
+ ] + [
+ MockProgrammingError(message)
+ for message in [
+ "(some pyodbc stuff) The cursor's connection has been closed.",
+ "(some pyodbc stuff) Attempt to use a closed connection."
+ ]
+ ]:
+ eq_(dialect.is_disconnect(error, None, None), True)
+
+ eq_(dialect.is_disconnect(
+ MockProgrammingError("not an error"), None, None), False)
+
@testing.requires.mssql_freetds
def test_bad_freetds_warning(self):
engine = engines.testing_engine()