提交 61f4b1ff 编写于 作者: M Matthew Draper

Make connection stealing more explicit

上级 f397b385
......@@ -415,7 +415,10 @@ def disconnect(raise_on_acquisition_timeout = true)
with_exclusively_acquired_all_connections(raise_on_acquisition_timeout) do
synchronize do
@connections.each do |conn|
checkin conn
if conn.in_use?
conn.steal!
checkin conn
end
conn.disconnect!
end
@connections = []
......@@ -447,7 +450,10 @@ def clear_reloadable_connections(raise_on_acquisition_timeout = true)
with_exclusively_acquired_all_connections(raise_on_acquisition_timeout) do
synchronize do
@connections.each do |conn|
checkin conn
if conn.in_use?
conn.steal!
checkin conn
end
conn.disconnect! if conn.requires_reloading?
end
@connections.delete_if(&:requires_reloading?)
......@@ -557,8 +563,7 @@ def reap
@connections.select do |conn|
conn.in_use? && !conn.owner.alive?
end.each do |conn|
conn.expire
conn.lease
conn.steal!
end
end
......
......@@ -185,7 +185,7 @@ def schema_cache=(cache)
# this method must only be called while holding connection pool's mutex
def expire
if in_use?
if @owner != Thread.current && @owner.alive?
if @owner != Thread.current
raise ActiveRecordError, "Cannot expire connection, " <<
"it is owned by a different thread: #{@owner}. " <<
"Current thread: #{Thread.current}."
......@@ -197,6 +197,19 @@ def expire
end
end
# this method must only be called while holding connection pool's mutex (and a desire for segfaults)
def steal! # :nodoc:
if in_use?
if @owner != Thread.current
pool.send :remove_connection_from_thread_cache, self, @owner
@owner = Thread.current
end
else
raise ActiveRecordError, 'Cannot steal connection, it is not currently leased.'
end
end
def unprepared_statement
old_prepared_statements, @prepared_statements = @prepared_statements, false
yield
......
......@@ -151,7 +151,7 @@ def test_reap_inactive
assert_equal 1, active_connections(@pool).size
ensure
@pool.connections.each(&:close)
@pool.connections.each { |conn| conn.close if conn.in_use? }
end
def test_remove_connection
......@@ -432,6 +432,9 @@ def test_bang_versions_of_disconnect_and_clear_reloadable_connections_if_unable_
Thread.new { @pool.send(group_action_method) }.join
# assert connection has been forcefully taken away from us
assert_not @pool.active_connection?
# make a new connection for with_connection to clean up
@pool.connection
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册