I’m using Sequel to talk to a MySQL database from a Rack webapp. Sometimes, the application sits idle long enough that it exceeds wait_timeout on the server (which we’ve left at the default 8 hours). The exception suggests we can enable autoReconnect, but apparently that’s not encouraged. Anyway, in that case, I’d still get an exception, and have to retry the operation.
If autoReconnect is a bad pattern, what pattern should I use? What I want to do is
DB["myDB"].pool.hold do | conn |
... do stuff
end
but of course that throws if the connection is stale. I can wrap the whole thing a begin-rescue and just try again, but if I have a large pool, I could end up retrying 10 or 20 times before I find one that works.
There are three possible solutions:
1) Increase the wait_timeout.
2) Use a cron job so that a connection is used more often than wait_timeout.
3) Use the connection_validation extension, which transparently handles reconnects if a disconnect is detected.