Recommend you build a singleton object that returns the db connection (your con
). The first time it is called, it performs the MySQLDB.connect()
. Then it keeps a static copy of the connection for returning to the caller on subsequent calls.
In order to recover from disconnects... First, note that a timeout is not the only reason for losing a connection; a network glitch could cause it. So forget about timeouts and simply plan to reconnect when needed.
One way is to do a ping
to see if its still open, and if it is not, reconnect.
Another way is to have "autoreconnect" turned on. But this has negative impact on transactions, @variables, and other thing that could bite you.