“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey
Senior Software Engineer, UK

ActiveRecord Connection Pool

We’ve been having this irritating problem appearing intermitantly on ferret based searches.

A ActiveRecord::ConnectionTimeoutError occurred in users#search:

  could not obtain a database connection within 5 seconds.  The max pool size is currently 5; consider increasing it.
  (druby:/localhost:9010) /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:198:in `checkout'

We have 8 mongrels running, so ideally we’ll need at least one connection to the database per mongrel. Since we could conceivably have 8 concurrent connections to the site. You can up the connection pool size by adding the pool connection option to database.yml. A quick check in the console will demonstrate that it is now set

rl@bloodandguts:~/myapp$ ./script/console 
Loading development environment (Rails 2.2.2)
>> ActiveRecord::Base.connection.instance_variable_get(:@config)
=> {:socket=>nil, :username=>"root", :adapter=>"mysql", :host=>"127.0.0.1", :database=>"e_learning_resource_production", :password=>nil, :pool=>8}

However, the problem persisted. I wasn’t aware that ferret itself maintains its own connection to the database so I’ll likely need to change this to 9. In fact we also have backgroundrb running which will also use its own connection. I think the lesson is to account for all the different processes that may be connecting the database at once when setting this option.

development:
  adapter: mysql
  database: blah_development
  username: blah
  password: blah
  host: localhost
  socket: /var/run/mysqld/mysqld.sock
  pool: 8
GPK of the Day Wrappin' RUTH