in reply to Reconnecting to a mysqldb
I have a long running script that needs to be able to reconnect to the database (mysql) since the db is restarted sometimes.
This makes me wonder: do there tend to be long intervals during which the long-running script isn't really doing anything with the database?
Have you considered "modularizing" the database connection and transactions, rather than creating and sustaining a single, constant connection? That is, if the usage pattern involves long intervals with no activity between occasional queries/updates/inserts, you might just want to open a fresh connection for each transaction. In this kind of situation, the overhead of creating a new connection each time is insignificant, and applying a suitable holding-loop ("keep trying till the server responds") would be pretty easy to nail down.
Of course, if the activity is fairly evenly distributed (and dense), then you need a sustained connection, with the kind of error trapping/auto-reconnect facility being discussed in the other replies.
|
|---|