Gracefully handling “MySQL has gone away”

前端 未结 1 770
太阳男子
太阳男子 2021-02-02 13:36

I\'m writing a small database adapter in Python, mostly for fun. I\'m trying to get the code to gracefully recover from a situation where the MySQL connection \"goes away,\" ak

相关标签:
1条回答
  • 2021-02-02 14:24

    Can't see from the code, but my guess would be that the db._get_cxn() method is doing some kind of connection pooling and returning the existing connection object instead of making a new one. Is there not a call you can make on db to flush the existing useless connection? (And should you really be calling an internal _-prefixed method?)

    For preventing MySQL has gone away I generally prefer to keep a timestamp with the connection of the last time I used it. Then before trying to use it again I look at the timestamp and close/discard the connection if it was last used more than a few hours ago. This saves wrapping every possible query with a try...except OperationalError...try again.

    0 讨论(0)
提交回复
热议问题