rails 5 的连接池

Connection Pool for rails 5

我在使用 rails 5 rc1 时遇到了这个问题。有谁知道如何在环境文件中配置它以及 rails 5 个活动记录的默认连接池大小是多少。

   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'

在所有 rails 版本中,我都使用了在 config/database.yml

中配置的连接池
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

所以只需增加它:

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

如果有帮助,请告诉我。

更新

似乎将您的值放入 environment/*.rb 文件并不是那么简单。 恕我直言,最接近的方法是按照@Alessandro Caetano 的建议使用 ENV 变量。

社区有一个 gem 用于此类操作:rais-dotenv

您可以只为每个环境创建 .env.* 个文件,然后 dotenv 将相应地加载它。

这是一个例子:

# .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

然后在你的 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'] %>

您可以在 config/database.yml 中设置连接池限制,如下所示:

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