mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
make active_connection? return true only if there is an open connection in use for the current thread. fixes #5330
This commit is contained in:
parent
8a714c4d80
commit
d523504c2f
2 changed files with 39 additions and 7 deletions
|
@ -128,10 +128,11 @@ module ActiveRecord
|
||||||
@reserved_connections[current_connection_id] ||= checkout
|
@reserved_connections[current_connection_id] ||= checkout
|
||||||
end
|
end
|
||||||
|
|
||||||
# Check to see if there is an active connection in this connection
|
# Is there an open connection that is being used for the current thread?
|
||||||
# pool.
|
|
||||||
def active_connection?
|
def active_connection?
|
||||||
active_connections.any?
|
@reserved_connections.fetch(current_connection_id) {
|
||||||
|
return false
|
||||||
|
}.in_use?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Signal that the thread is finished with the current connection.
|
# Signal that the thread is finished with the current connection.
|
||||||
|
@ -288,10 +289,6 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
c
|
c
|
||||||
end
|
end
|
||||||
|
|
||||||
def active_connections
|
|
||||||
@connections.find_all { |c| c.in_use? }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# ConnectionHandler is a collection of ConnectionPool objects. It is used
|
# ConnectionHandler is a collection of ConnectionPool objects. It is used
|
||||||
|
|
|
@ -3,6 +3,8 @@ require "cases/helper"
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
module ConnectionAdapters
|
module ConnectionAdapters
|
||||||
class ConnectionPoolTest < ActiveRecord::TestCase
|
class ConnectionPoolTest < ActiveRecord::TestCase
|
||||||
|
attr_reader :pool
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
|
|
||||||
|
@ -25,6 +27,39 @@ module ActiveRecord
|
||||||
@pool.disconnect!
|
@pool.disconnect!
|
||||||
end
|
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
|
def test_full_pool_exception
|
||||||
assert_raises(PoolFullError) do
|
assert_raises(PoolFullError) do
|
||||||
(@pool.size + 1).times do
|
(@pool.size + 1).times do
|
||||||
|
|
Loading…
Reference in a new issue