开发者

sqlalchemy pymssql "connection reset by peer" recovery

开发者 https://www.devze.com 2023-02-13 13:29 出处:网络
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 resta开发者_运维问答r

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 resta开发者_运维问答rt the webservice.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消