sqlalchemy pymssql “connection reset by peer” recovery

不羁的心 提交于 2019-12-10 11:50:14

问题


I'm running a cherrypy webservice and wondering what the best option is to recover from "connection reset by peer" for a pymssql connection via sqlalchemy. Right now I have to restart the webservice.


回答1:


This seems to be a bug in the is_disconnect() method for pymssql where it ignore TCP connection and timeout failures, leaving the cursor in an unhappy state; see http://www.sqlalchemy.org/trac/ticket/2172. For now, you can monkey-patch as:

from sqlalchemy.dialects.mssql import pymssql

def is_disconnect(self, e):
    for msg in (
        "20003",
        "20004",
        "Error 10054",
        "Not connected to any MS SQL server",
        "Connection is closed"
        ):
        if msg in str(e):
            return True
    else:
        return False

pymssql.MSDialect_pymssql.is_disconnect = is_disconnect


来源:https://stackoverflow.com/questions/5095555/sqlalchemy-pymssql-connection-reset-by-peer-recovery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!