简体   繁体   English

如何确定在Rails中随时打开哪些数据库连接?

[英]How do I figure out what database connections are open at any one time in Rails?

I'm running a single-threaded Rails 5.0 application through the "rails console", with the below configuration in config/database.yml for my development environment: 我正在通过“ rails控制台”运行单线程Rails 5.0应用程序,并在我的开发环境中使用config/database.yml的以下配置:

development:
  adapter: postgresql
  encoding: utf8
  database: sims 
  username: postgres
  password: password
  pool: 5
  timeout: 15000
  host: 127.0.0.1

Invariably, running my code below will eventually die with a "Could not obtain a connection" error: 始终,在下面运行我的代码最终将因“无法获取连接”错误而死亡:

  pool = Concurrent::FixedThreadPool.new(1) 
  promises = links.map do |link| 
    Concurrent::Promise.execute(executor: pool) do
      result = process_link(link) 
      if result
        if result.kind_of?(Array) 
          result.each do |my_obj|
            my_obj.update_attributes({ :a => a }) 
            records_processed = records_processed + my_obj.matches.count 
          end
        else
          records_processed = records_processed + result.matches.count 
          result.update_attributes({ :a => a }) 
        end
      end
    end
  end
  promises.map(&:wait).map(&:value!)

How can I figure out what connections are not being closed, or what queries are still running that would cause this to occur? 如何确定未关闭哪些连接,或者哪些查询仍在运行,这会导致这种情况发生? I am baffled because nowhere should concurrent queries be running at once. 我感到困惑,因为并发查询不应在任何地方一次运行。

Error during processing: (ActiveRecord::ConnectionTimeoutError) could not obtain a connection from the pool within 5.000 seconds (waited 5.003 seconds); all pooled connections were in use
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:202:in `block in wait_poll'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `loop'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `wait_poll'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:154:in `internal_poll'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:278:in `internal_poll'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `block in poll'
/Users/nataliab/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:158:in `synchronize'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `poll'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:717:in `acquire_connection'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:490:in `checkout'
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:364:in `connection'

您可以使用以下方法从PostgreSQL获取此信息:

SELECT * from pg_stat_activity;

I'm running a single-threaded Rails 5.0 application 我正在运行一个单线程的Rails 5.0应用程序

As soon as your application code creates another thread (which it is doing via Concurrent::FixedThreadPool.new(1) ), it is no longer a 'single-threaded application'. 一旦您的应用程序代码创建了另一个线程(通过Concurrent::FixedThreadPool.new(1) ),它就不再是“单线程应用程序”。

How can I figure out what connections are not being closed or what queries are still running that would cause this to occur. 我如何确定哪些连接没有关闭或哪些查询仍在运行,这会导致这种情况发生。

To inspect the connections that are not being closed, you can access the current connection pool's list of connections via ActiveRecord::Base.connection_pool.connections , which will return an Array of ActiveRecord::ConnectionAdapters::AbstractAdapter objects you can examine. 要检查未关闭的连接,可以通过ActiveRecord::Base.connection_pool.connections访问当前连接池的连接列表,该列表将返回一个ActiveRecord::ConnectionAdapters::AbstractAdapter对象数组。 For example, to inspect the thread owner of each connection currently in the connection pool, you can run: 例如,要inspect连接池中当前每个连接的线程owner ,可以运行:

ActiveRecord::Base.connection_pool.connections.map(&:owner).map(&:inspect)

I am baffled because nowhere should concurrent queries be running at once. 我感到困惑,因为并发查询不应在任何地方一次运行。

Your assumption that concurrent queries are running because the connection pool is being exhausted is not correct. 您认为由于连接池已耗尽而正在运行并发查询的假设是不正确的。 On how to fix this issue, refer to my answer to your other (very similar) question, Does concurrency happen even when only one thread is in a thread pool? 关于如何解决此问题,请参考我对另一个(非常相似)问题的回答: 即使线程池中只有一个线程并发也会发生吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 rails中的线程是否会自动打开数据库连接? - Do threads in rails automatically open database connections? 如何防止数据库连接在Rails中超时? - How can I prevent database connections from timing out in Rails? 我怎么弄清楚为什么我的rails 3应用程序,使用mod_rails,是如此之慢? - How do I figure out why my rails 3 app, using mod_rails, is so slow? 在 Rails 中进行每用户数据库连接的最佳方法是什么 - What is the best way to do per-user database connections in Rails 我如何从 jquery-ui-rails gem 版本中找出 JQuery UI 的版本? - How do i figure out the version of JQuery UI from version of jquery-ui-rails gem? 在Rails中,如何确定对象数组是否包含与给定值匹配的特定属性? - In Rails, how do I figure out if an array of objects contains specific attributes matching given values? 我怎么弄清楚哪些其他JS文件干扰了我关注的一个? - How do I figure out which other JS files are interfering with one I am concerned about? Rails:如何找出活动记录中的字段 - Rails: How do I find out what fields are in an active record 无法激活gem:如何确定哪个gem依赖于此gem? - Can't activate gem: how do I figure out which gem has a dependency on this one? 我如何找出在heroku上运行的found-elasticsearch版本? - How do I figure out what found-elasticsearch version is running on heroku?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM