简体   繁体   中英

Connection pool error in Rails application with Postgres

I am having an issue when my Rails app is deployed in AWS ECS. Many of requests finish with 200 but there are some (every let's say 50) finish with 500 . The log says:

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

I changed my pool (in database.yml config) to 15, also set RAILS_MAX_THREADS to 15, and indeed it happens rarely now but still the problem occurs.

What am I missing here?

Ruby version: 2.6.6 Postgres: 11.5

database.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

Try and use this have common values in default and then depending upon env have individual values for the other environments, which can be based on environment variables:

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'] %>

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