diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-03-08 15:40:23 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-03-08 15:40:23 -0800 |
commit | d523504c2f18e459e21a8cfa019ef0620db61b79 (patch) | |
tree | 87d7d68299144a8670d83ceaf4a19fb8b3d7017c | |
parent | 8a714c4d804d2502c4b1bdfccb3e9175c4add7b8 (diff) | |
download | rails-d523504c2f18e459e21a8cfa019ef0620db61b79.tar.gz rails-d523504c2f18e459e21a8cfa019ef0620db61b79.tar.bz2 rails-d523504c2f18e459e21a8cfa019ef0620db61b79.zip |
make active_connection? return true only if there is an open connection in use for the current thread. fixes #5330
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/connection_pool_test.rb | 35 |
2 files changed, 39 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 37a9d216df..eb88f191a7 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -128,10 +128,11 @@ module ActiveRecord @reserved_connections[current_connection_id] ||= checkout end - # Check to see if there is an active connection in this connection - # pool. + # Is there an open connection that is being used for the current thread? def active_connection? - active_connections.any? + @reserved_connections.fetch(current_connection_id) { + return false + }.in_use? end # Signal that the thread is finished with the current connection. @@ -288,10 +289,6 @@ module ActiveRecord end c end - - def active_connections - @connections.find_all { |c| c.in_use? } - end end # ConnectionHandler is a collection of ConnectionPool objects. It is used diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb index 2c69bfde5b..25375c5f7a 100644 --- a/activerecord/test/cases/connection_pool_test.rb +++ b/activerecord/test/cases/connection_pool_test.rb @@ -3,6 +3,8 @@ require "cases/helper" module ActiveRecord module ConnectionAdapters class ConnectionPoolTest < ActiveRecord::TestCase + attr_reader :pool + def setup super @@ -25,6 +27,39 @@ module ActiveRecord @pool.disconnect! end + def active_connections(pool) + pool.connections.find_all(&:in_use?) + end + + def test_with_connection + assert_equal 0, active_connections(pool).size + + main_thread = pool.connection + assert_equal 1, active_connections(pool).size + + Thread.new { + pool.with_connection do |conn| + assert conn + assert_equal 2, active_connections(pool).size + end + assert_equal 1, active_connections(pool).size + }.join + + main_thread.close + assert_equal 0, active_connections(pool).size + end + + def test_active_connection_in_use + assert !pool.active_connection? + main_thread = pool.connection + + assert pool.active_connection? + + main_thread.close + + assert !pool.active_connection? + end + def test_full_pool_exception assert_raises(PoolFullError) do (@pool.size + 1).times do |