【问题标题】:How to fix a DB error in a Rails PostgreSQL app?如何修复 Rails PostgreSQL 应用程序中的数据库错误?
【发布时间】:2019-12-20 20:00:46
【问题描述】:

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

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

到目前为止我尝试过:

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

谢谢。

【问题讨论】:

  • 当前数据库池大小是多少?您可以同时与您的 web 应用程序交互的最大用户数是多少?你在用独角兽吗?
  • 您的问题不在于数据库配置,而在于线程未释放连接。就像上面提到的 Rustem 一样,有多少请求同时到达您的应用程序?不过你可以暂时使用pgbouncer

标签: ruby-on-rails postgresql amazon-web-services amazon-rds


【解决方案1】:

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

如果您使用 Unicorn pre-fork 服务器模型,则可以配置 before/after 挂钩以优雅地打开和关闭数据库连接。

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

【讨论】:

    猜你喜欢
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    相关资源
    最近更新 更多