Change ActiveRecord Pool Size on the Fly

I have a rake task that runs using the application’s production environment. Unlike the production environment though, it is threaded. In order to make any real use out of that, I need to increase the ActiveRecord connection pool size. Normally you’d do this in your database.yml, but in this case I’d rather not modify the production environment’s settings for the sake of a single rake task.

Here’s how you can make the change, on the fly, inside of your rake task:

task :swimming_pool => :environment do
  ActiveRecord::Base.connection_pool.instance_variable_set('@size', 15)
  ActiveRecord::Base.connection_pool.instance_variable_set('@timeout', 10)
  # Do awesome threaded stuff here
end

The first line of the rake task increases your pool size and the second line changes your timeout when waiting for a connection from the pool. You can adjust those values to whatever you need. That just happened to be what I need for this particular task.

Posted in Code, ruby at June 16th, 2010. Trackback URI: trackback
Tags: , , , , , ,

No Responses to “Change ActiveRecord Pool Size on the Fly”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>