Skip to content Skip to sidebar Skip to footer

Gracefully Handling "mysql Has Gone Away"

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,' aka wait_

Solution 1:

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.

Post a Comment for "Gracefully Handling "mysql Has Gone Away""