Connection Pool for rails 5

15,138

Solution 1

In all rails versions I had used connection pool configured in config/database.yml

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

So just increase it:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 10
  timeout: 5000

Let me know if it will be helpful.

UPDATE

It seems it's not so straightforward to put your values to environment/*.rb files. The closest way IMHO is to use ENV variables as @Alessandro Caetano suggests.

Community has a gem for such operations: rais-dotenv

You could just create .env.* files for each environment and then dotenv will load it accordingly.

Here is an example:

# .env.development
main_db_database=main_db_development
main_db_pool=5
main_db_host=localhost
main_db_port=3306
main_db_user=user
main_db_password=password

Then in your database.tml

development: &main_db
  adapter: mysql2
  encoding: utf8
  reconnect: true
  database: <%= ENV['main_db_database'] %>
  pool: <%= ENV['main_db_pool'] ? ENV['main_db_pool'].to_i : 5 %>
  host: <%= ENV['main_db_host'] %>
  port: <%= ENV['main_db_port'] %>
  username: <%= ENV['main_db_username'] %>
  password: <%= ENV['main_db_password'] %>

Solution 2

You can set the connection pool limit in your config/database.yml, like this:

production:
   url:  <%= ENV["DATABASE_URL"] %>
   pool: <%= ENV["DB_POOL"] || ENV['RAILS_MAX_THREADS'] || 5 %>
Share:
15,138

Related videos on Youtube

Author by

sethi

A developer mostly interested in C++ , Java and Hadoop

Updated on August 18, 2022

Comments

  • sethi 3 months

    I am having this issue with rails 5 rc1. Does anyone have any idea how to configure it in the environment files and what is the default connection pool size for rails 5 active record.

       Puma caught this error: could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use (ActiveRecord::ConnectionTimeoutError)
        /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:202:in `block in wait_poll'
        /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `loop'
        /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `wait_poll'
        /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:154:in `internal_poll'
        /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:278:in `internal_poll'
        /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `block in poll'
    
  • typeoneerror
    typeoneerror over 5 years
    .to_i doesn't appear to be necessary on the pool size github.com/rails/rails/blob/4-2-stable/activerecord/lib/…
  • Nicolas Guillaume
    Nicolas Guillaume about 4 years
    Do you just add ENV["DB_POOL"] = x to the boot.rb file?
  • alessandrocb
    alessandrocb about 4 years
    @NicolasGuillaume you can set these variables on your ~/.bashrc file using export DB_POOL = 5 and so on. After that, you need tosource ~/.bashrc to apply the changes to your current terminal session or open a new terminal, then your rails app should be able to recognize the environment variable. If you don't like putting enviroment variables on your bashrc, you can use a gem, as @retgoat sugests.
  • whodini9 about 4 years
    You can also use github.com/laserlemon/figaro which stores unique environment variables in config/application.yml and can raise errors if variables are missing.

Related