2 Posts in September, 2009 Archive
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.
Doozer 0.1.5 Released
I added project configuration support for loading a specific Doozer gem version this evening.
All you need to do is declare the version of the gem your project requires and it automatically loads it for you. This is facilitated from 'config/boot.rb' which handles the loading. I have one small change which isn't committed to load 'grippy-doozer' (github gem version) if it exists. I'll commit this with the next patch.
Next up: Turn some of the project generator templates into Erb files to make them easier to maintain.