简体   繁体   中英

Connection Pool for rails 5

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'

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'] %>

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 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM