Archive for the ‘MySQL’ Category

In my current Ruby n Rails project, the focus of work is to periodically update database entries from XML feeds.

Naturally I’ve implemented a threading system using the rufus-scheduler gem for updating database entries every so many minutes. However I soon noticed that after running on the development enviroment with 1 minute intervals on updates, that the updating of the last updated time column in the db suddenly stopped after about 5 iterations. No error, warnings or other signals (smoke or otherwise).

After a bit of head scratching I finally managed to mentally link the 5 iterations to the pool number setting in the database.yml file. Executing my database updates within the thread job was stopping Rails from cleaning up the unrequired connections (for whatever reason. I suspect this is because the thread is sent back to sleep before this can occur).

So the solution is to simply call the cleanup manually via…

ActiveRecord::Base.connection_pool.clear_stale_cached_connections!

… at the end of every job execution run. Problem sorted!