From: Chuck Remes Date: 2009-09-10T06:59:01+09:00 Subject: Re: EventMachine.defer and ActiveRecord connection pool? On Sep 9, 2009, at 4:43 PM, Artūras Šlajus wrote: > Chuck Remes wrote: >> On Sep 9, 2009, at 4:14 PM, Artūras Šlajus wrote: >> >>> Nop, it's 1 second for testing. It happens even if you set it to 1 >>> minute. Loads of time to clean up and still... >>> >> >> Hmm, that is curious. Then here's another guess... >> >> I haven't used rails in a long time, but I recall that AR required a >> call to verify_connections! or something crazy when running with >> threads. > Tried this with no luck :) Thou it gave me some idea for googling... > >> This is because each call opens its own connection to the >> database but there is (or was) no automatic close. So it could be >> that >> your threaded calls are opening up db connections and never closing >> them. You need to figure out how to close the connection or get AR to >> figure out that the others are stale or should be garbage collected. > > It seems that ActiveRecord::Base.clear_reloadable_connections! in: > > EventMachine.defer( > proc do > SingletonBlock.started(name) > LOGGER.block(name) { operation.call } > end, > proc do > LOGGER.block("#{name} CALLBACK") { callback.call } if > callback > SingletonBlock.finished(name) > ActiveRecord::Base.clear_reloadable_connections! > end > ) > > actually helped :) > (http://coderrr.wordpress.com/2009/01/12/rails-22-activerecord-connection-cleanup/ > ) > > Thanks for ideas and help! ;) You are welcome. This made me curious so I looked a little deeper and found a better choice for you. Look up #release_connection in ActiveRecord::ConnectionAdapters::ConnectionPool. It does exactly what you need whereas that call to #clear_reloadable_connections! might be doing unnecessary work. Good luck. cr