我正在使用 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/