Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 504
sethi Avatar asked Jun 24 '16 02:06

sethi


People also ask

What is connection pool in rails?

A connection pool synchronizes thread access to a limited number of database connections. The basic idea is that each thread checks out a database connection from the pool, uses that connection, and checks the connection back in.

What is connection pool size?

A connection pool maintains a specific number of open database connections to an application in the main memory. The default size of the database connection pool is eight, but you can change this number while deploying or updating an application in the SAP BTP cockpit.

How do I set up a connection pool in rails?

In your Rails application, the config/database.yml file contains the pool option. As explained in the Rails docs: Active Record database connections are managed by ActiveRecord::ConnectionAdapters::ConnectionPool, which ensures that a connection pool synchronizes the amount of thread access to a limited number of database connections.

What is connectionpool and how does it work?

The basic idea is that each thread checks out a database connection from the pool, uses that connection, and checks the connection back in. ConnectionPool is completely thread-safe, and will ensure that a connection cannot be used by two threads at the same time, as long as ConnectionPool's contract is correctly followed.

How to check max pool size in rails?

Max pool size is controlled by the pool option in config/database.yml. By default it can be overridden by ENV ["RAILS_MAX_THREADS"]. You can check max pool size via:

How does connection pooling work in active record?

Since the connection pooling is handled inside of Active Record by default, all application servers (Thin, Puma, Unicorn, etc.) should behave the same. The database connection pool is initially empty. As demand for connections increases, it will create them until it reaches the connection pool limit.


1 Answers

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'] %>
like image 64
retgoat Avatar answered Oct 05 '22 00:10

retgoat