How to avoid losing mysql connections with Rails

HI everyone,

What’s the current best practice for avoiding ActiveRecord mySQL errors
like these two here?

ActiveRecord::StatementInvalid: Mysql::Error: MySQL server has gone


ActiveRecord::StatementInvalid: Mysql::Error: Lost connection to MySQL
server during query

I have an app that’s just sent me a few such errors in quick succession,
and I’m trying to find the best way to stop this happening again, and
Im’ somewhat confused about what most of you guys tend to do when faced
with an error like this.

The easiest option, to add a ‘reconnect: true’ flag to database.yaml
seems to be something that’s advised against in the MySQL

The other first option that comes to me to put a reconnect function in
the :before filter, doesn’t seem to work too well either according to
this guy[3] either, and I’m not familiar enough with Rails’s source to
try hacking around without some guidance.

I think these are happening when a MySQL connection times out without
the rails process picking up on this, but I’m not sure.

Would some kind soul help shed some light on the best course of action

Many thanks,


[3] Rails and mysql timeouts | Notes In The Margin