python: sqlalchemy - how do I ensure connection not stale using new event system

后端 未结 1 1567
暖寄归人
暖寄归人 2021-02-10 17:27

I am using the sqlalchemy package in python. I have an operation that takes some time to execute after I perform an autoload on an existing table. This causes the following erro

相关标签:
1条回答
  • 2021-02-10 18:23

    I think you are looking for something like this:

    from sqlalchemy import exc, event
    from sqlalchemy.pool import Pool
    
    @event.listens_for(Pool, "checkout")
    def check_connection(dbapi_con, con_record, con_proxy):
        '''Listener for Pool checkout events that pings every connection before using.
        Implements pessimistic disconnect handling strategy. See also:
        http://docs.sqlalchemy.org/en/rel_0_8/core/pooling.html#disconnect-handling-pessimistic'''
    
        cursor = dbapi_con.cursor()
        try:
            cursor.execute("SELECT 1")  # could also be dbapi_con.ping(),
                                        # not sure what is better
        except exc.OperationalError, ex:
            if ex.args[0] in (2006,   # MySQL server has gone away
                              2013,   # Lost connection to MySQL server during query
                              2055):  # Lost connection to MySQL server at '%s', system error: %d
                # caught by pool, which will retry with a new connection
                raise exc.DisconnectionError()
            else:
                raise
    

    If you wish to trigger this strategy conditionally, you should avoid use of decorator here and instead register listener using listen() function:

    # somewhere during app initialization
    if config.check_connection_on_checkout:
        event.listen(Pool, "checkout", check_connection)
    

    More info:

    • Connection Pool Events
    • Events API
    0 讨论(0)
提交回复
热议问题