如何修复 Rails PostgreSQL 应用程序中的数据库错误?



我想知道如何在连接到 AWS 上的 RDS 数据库的 Rails 6 应用程序中修复以下错误:

无法在 5.000 秒内从池中获取连接 (等待 5.075 秒(;所有池连接都在使用

到目前为止,我尝试了:

  • 在数据库.yml 中将池增加到 100
  • 从 2 升级数据库实例 vCPU 到 4 个 CPU。内存为 16 GB

谢谢。

我建议 https://devcenter.heroku.com/articles/forked-pg-connections 通过此链接。至少,验证 Postgres 服务器可以处理的活动连接数的限制。

如果使用 Unicorn 预分叉服务器模型,则可以配置之前/之后的钩子以正常打开和关闭数据库连接。

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

最新更新