简体   繁体   中英

How to fix a DB error in a Rails PostgreSQL app?

I would like to know how to fix the following error, in a Rails 6 app, that is connected to an RDS database at AWS:

could not obtain a connection from the pool within 5.000 seconds (waited 5.075 seconds); all pooled connections were in use

So far I tried:

  • Increasing pool to 100 in database.yml
  • Upgrading db instance from 2 vcpus to 4 cpus. The RAM is 16 GB

Thanks.

I would recommend going through this link https://devcenter.heroku.com/articles/forked-pg-connections . At least, validate limit for number of active connections that your Postgres server can handle.

If you use Unicorn pre-fork server model, then may be configure before/after hooks to gracefully open and close database connections.

before_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection(
      Rails.application.config.database_configuration[Rails.env]
    )

end

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