提交 0e2477b6 编写于 作者: A Aaron Patterson

Automatic closure of connections in threads is deprecated. For example

the following code is deprecated:

Thread.new { Post.find(1) }.join

It should be changed to close the database connection at the end of
the thread:

Thread.new {
  Post.find(1)
  Post.connection.close
}.join

Only people who spawn threads in their application code need to worry
about this change.
上级 29d2040b
## Rails 3.2.0 (unreleased) ##
* Automatic closure of connections in threads is deprecated. For example
the following code is deprecated:
Thread.new { Post.find(1) }.join
It should be changed to close the database connection at the end of
the thread:
Thread.new {
Post.find(1)
Post.connection.close
}.join
Only people who spawn threads in their application code need to worry
about this change.
* Deprecated:
* `set_table_name`
......
......@@ -190,7 +190,13 @@ def clear_stale_cached_connections!
t.alive?
}.map { |thread| thread.object_id }
keys.each do |key|
checkin @reserved_connections[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
......
......@@ -26,30 +26,6 @@ def test_active_connection?
assert !@pool.active_connection?
end
def test_clear_stale_cached_connections!
pool = ConnectionPool.new ActiveRecord::Base.connection_pool.spec
threads = [
Thread.new { pool.connection },
Thread.new { pool.connection }]
threads.map { |t| t.join }
pool.extend Module.new {
attr_accessor :checkins
def checkin conn
@checkins << conn
conn.object_id
end
}
pool.checkins = []
cleared_threads = pool.clear_stale_cached_connections!
assert((cleared_threads - threads.map { |x| x.object_id }).empty?,
"threads should have been removed")
assert_equal pool.checkins.length, threads.length
end
def test_checkout_behaviour
pool = ConnectionPool.new ActiveRecord::Base.connection_pool.spec
connection = pool.connection
......@@ -70,12 +46,15 @@ def test_checkout_behaviour
assert thread_ids.include?(t.object_id)
end
pool.connection
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
end.join()
pool.connection.close
end.join
end
......
......@@ -563,6 +563,7 @@ def test_transaction_per_thread
topic.approved = !topic.approved?
topic.save!
end
Topic.connection.close
end
end
......@@ -598,6 +599,7 @@ def test_transaction_isolation__read_committed
dev = Developer.find(1)
assert_equal original_salary, dev.salary
end
Developer.connection.close
end
end
......@@ -610,6 +612,7 @@ def test_transaction_isolation__read_committed
assert_equal original_salary, Developer.find(1).salary
end
end
Developer.connection.close
end
threads.each { |t| t.join }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册