【问题标题】:Connection pool error in Rails application with Postgres使用 Postgres 的 Rails 应用程序中的连接池错误
【发布时间】:2020-05-25 13:19:28
【问题描述】:

当我的 Rails 应用程序部署在 AWS ECS 中时,我遇到了问题。许多请求以200 结束,但也有一些(例如50 个)以500 结束。日志说:

ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.)

我将池(在database.yml 配置中)更改为 15,还将 RAILS_MAX_THREADS 设置为 15,确实现在很少发生,但问题仍然存在。

我在这里错过了什么?

Ruby 版本:2.6.6 Postgres:11.5

数据库.yml:

production:
  adapter: postgresql
  encoding: utf8
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOSTNAME'] %>
  port: <%= ENV['DB_PORT'] %>
  pool: 25

【问题讨论】:

  • 愿意分享 database.yml 吗?
  • 不好意思之前没做过,问题更新了
  • 你也安装了pg gem吗?
  • 是的,已经安装了
  • 你能看看我下面粘贴的yml吗

标签: ruby-on-rails ruby postgresql


【解决方案1】:

尝试使用这个默认的通用值,然后根据 env 为其他环境设置单独的值,这可以基于环境变量:

default: &default
  adapter:  postgresql
  encoding: unicode
  port:     <%= ENV.fetch("POSTGRESQL_PORT", "5432") %>
  pool:     <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['POSTGRESQL_USER_NAME'] %>
  password: <%= ENV.fetch("POSTGRESQL_PASSWORD", "somepassword") %>
  host:     <%= ENV['POSTGRESQL_HOST'] %>

development:
  <<: *default
  database: <%= ENV['POSTGRESQL_DB'] %>-development
  host: db

production:
  <<: *default
  database: <%= ENV['POSTGRESQL_DB'] %>

【讨论】:

  • Unfo 问题仍然存在。
  • 您有任何 rake 任务正在运行吗?如果有,那么可能是您在运行 rake 任务之前没有运行环境任务,任务 some_task: : environment do end
猜你喜欢
  • 2011-07-02
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多