ActiveRecord and The Dreaded RuntimeError: Mysql::Error: MySQL server has gone away
I've been running Doozer on mzk.me since starting the Doozer project. Mzk is configured to use ActiveRecord for its ORM and is not anything other then a simple url shortening service (you know they're all the rage these days?).
After scanning my log files one day, I came across the following error:
RuntimeError: Mysql::Error: MySQL server has gone away: SELECT * FROM `redirects` WHERE (`redirects`.`token` = 'b') LIMIT 1
This error meant the site was dead in the water. ARRRGGGHHH!!! Now, I like water, but not dead things in the water. Thankfully, the error led me in the right direction.
It turns out: MySQL defaults to 8hrs. of inactivity before it releases all open connections. See here and here. To adjust this value you need to configure the 'wait_timeout' variable.
Logically, this seems like something ActiveRecord should check if a connection exists and open one if it doesn't. Having never come across this error before (I know one of the side effects of having a not-so-popular domain) I automatically assumed ActiveRecord would handle this and open a fresh connection.
ActiveRecord has a few deprecated methods #allow_concurrency and #verification_timeout which may have solved this at one time, but no longer do or list alternate solutions.
In the end, I ended up taking the hackish way out and setting up a cronjob to ping the webserver every 6 hours to reset the wait timeout period. This solved the immediate problem but wasn't the solution I was looking for.
blog comments powered by Disqus