[英]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.