ruby-on-rails - Rails 控制台中的 Postgres 连接过多

标签 ruby-on-rails ruby database postgresql rubygems

我正在使用 postgre gem 开发 Ruby on Rails 应用程序,这就是我的 database.yml 的样子:

development:
 adapter: postgresql
  encoding: utf-8
  pool: 5
  username: "hytxlzju"
  password: "xxxxx"
  host: "jumbo.db.elephantsql.com"
  port: "5432"
  database: "hytxlzju"

production:
  adapter: postgresql
  encoding: utf-8
  pool: 5
  username: "hytxlzju"
  password: "xxxxxx"
  host: "jumbo.db.elephantsql.com"
  port: "5432"
  database: "hytxlzju"

每当我从 rails console 本地连接到这个数据库时,我得到的连接太多了。我如何在代码中终止连接,一旦用户注销,在代码中,以及如何在我的 Rails 控制台中终止连接,在我完成更改表?

[编辑] 这是错误消息:

C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22.5/lib/active_record/connection_adapters/postgresql_adapter.rb:12
22:in `initialize': FATAL:  too many connections for role "hytxlzju" (PG::ConnectionBad)

[编辑] 我添加了我的启动器,仍然没有成功:

Rails.application.config.after_initialize do
  ActiveRecord::Base.connection_pool.disconnect!

  ActiveSupport.on_load(:active_record) do
    config = ActiveRecord::Base.configurations[Rails.env] ||
                Rails.application.config.database_configuration[Rails.env]
    config['pool']              = ENV['DB_POOL']      || ENV['RAILS_MAX_THREADS'] || 5
    ActiveRecord::Base.establish_connection(config)
  end
end

最佳答案

你可以试试下面的方法

Active Record limits the total number of connections per application through a database setting pool; this is the maximum size of the connections your app can have to the database

config/datbase.yml

pool: <%= ENV['RAILS_MAX_THREADS'] || 5 %>

如果您使用的是 puma,则使用 ENV['RAILS_MAX_THREADS'] more here

它可能会解决问题。

关于ruby-on-rails - Rails 控制台中的 Postgres 连接过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40426670/

相关文章:

ruby - Rest Client 是否支持 NTLM Auth?

ruby-on-rails - 了解如何使用 Pundit

c# - 通过 System.Data.SQLite 和 c# 对单个 SQLite 数据库文件进行多次访问

database - Google App Engine 数据库值不递增

ruby-on-rails - rails/Postgres : Error after changing pg gem version

ruby-on-rails - Where(Hash) 后跟 ActiveRecord 中的 Scoped Where(Hash)

mysql - 按定制选择 rails 订购

ruby-on-rails - 如何设置 rails f.select 的 "data-"

java - mybatis映射中如何定义复合主键

ruby-on-rails - 运行rails服务器时没有 'jcode' gem