提交 67692939 编写于 作者: A Aaron Patterson

connections must be checked in at the end of a thread

上级 7c55d697
## Rails 4.0.0 (unreleased) ##
* Connections *must* be closed at the end of a thread. If not, your
connection pool can fill and an exception will be raised.
* Added the `ActiveRecord::Model` module which can be included in a
class as an alternative to inheriting from `ActiveRecord::Base`:
......
......@@ -155,7 +155,6 @@ def clear_reloadable_connections!
# associated with stale threads.
def verify_active_connections! #:nodoc:
synchronize do
clear_stale_cached_connections!
@connections.each do |connection|
connection.verify!
end
......@@ -165,20 +164,8 @@ def verify_active_connections! #:nodoc:
# Return any checked-out connections back to the pool by threads that
# are no longer alive.
def clear_stale_cached_connections!
keys = @reserved_connections.keys - Thread.list.find_all { |t|
t.alive?
}.map { |thread| thread.object_id }
keys.each do |key|
conn = @reserved_connections[key]
ActiveSupport::Deprecation.warn(<<-eowarn) if conn.in_use?
Database connections will not be closed automatically, please close your
database connection at the end of the thread by calling `close` on your
connection. For example: ActiveRecord::Base.connection.close
eowarn
checkin conn
@reserved_connections.delete(key)
end
end
deprecate :clear_stale_cached_connections!
# Check-out a database connection from the pool, indicating that you want
# to use it. You should call #checkin when you no longer need this.
......@@ -214,12 +201,9 @@ def checkout
return conn
end
@queue.wait(@timeout)
if(active_connections.size < @connections.size)
next
else
clear_stale_cached_connections!
if @size == active_connections.size
raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it."
end
......
......@@ -35,27 +35,16 @@ def test_checkout_behaviour
threads << Thread.new(i) do |pool_count|
connection = pool.connection
assert_not_nil connection
connection.close
end
end
threads.each {|t| t.join}
threads.each(&:join)
Thread.new do
threads.each do |t|
thread_ids = pool.instance_variable_get(:@reserved_connections).keys
assert thread_ids.include?(t.object_id)
end
assert_deprecated do
pool.connection
end
threads.each do |t|
thread_ids = pool.instance_variable_get(:@reserved_connections).keys
assert !thread_ids.include?(t.object_id)
end
assert pool.connection
pool.connection.close
end.join
end
def test_automatic_reconnect=
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册