Rails ActiveRecord w/ MySQL Hangs Once Connection Pool Limit is Reached

activerecordruby-on-rails

Unless specified, ActiveRecord fires up 4 default connections.

I've noticed if I reload a simple page, which makes at least one ActiveRecord call, 4 times every thing functions normal. However, on the fifth time, it hangs for almost 5 seconds. So, every 5th page load it hangs for 5 seconds.

I upped the default connection pool in my adapter to 8 and now it hangs every 9th page request. It seems like it doesn't reuse connections but instead kills them and regenerates them once the pool limit is reached.

I'm not actually using ActiveRecord with entire Rails stack.

Is there a magical ActiveRecord connection flag which prevents this from happening? Or, is there something else going wrong?

Best Answer

Hot damn that was easy...

ActiveRecord::Base.clear_active_connections!

http://coderrr.wordpress.com/2009/01/12/rails-22-activerecord-connection-cleanup/

Sure wish this was something you could configure so you don't have to call it every time.