【问题标题】:Postgree too many connections in rails consolePostgres在rails控制台中的连接太多
【发布时间】:2017-03-18 12:33:45
【问题描述】:

我正在使用 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 本地连接到这个数据库时,我都会获得太多连接。如何在用户注销后在代码中杀死一个连接,以及在完成更改表?

[编辑] 这是错误信息:

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

【问题讨论】:

    标签: ruby-on-rails ruby database postgresql rubygems


    【解决方案1】:

    你可以试试下面的方法

    Active Record 限制每个应用程序的连接总数 通过数据库设置pool;这是最大尺寸 您的应用与数据库的连接

    在config/datbase.yml

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

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

    或许能解决问题。

    【讨论】:

    • 我怎样才能杀死这个连接?也许这会解决它
    【解决方案2】:

    [已解决]

    不知何故,我的演示应用程序没有找到表中的条目,因此它创建了多个池连接,但没有关闭它们,因为在关闭连接之前,抛出了 500 错误,因此我关闭了那段代码游泳池从未关闭。更多关于 postgre 会话在这里 https://devcenter.heroku.com/articles/concurrency-and-database-connections#connection-pool

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 2014-01-03
      • 1970-01-01
      相关资源
      最近更新 更多