diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-12-30 11:37:21 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-12-30 11:37:21 -0800 |
commit | 6769293988c0d7733138a562aaa683375cbdcb78 (patch) | |
tree | aa367428777c10e792384b79e81a712785393722 | |
parent | 7c55d6977c68041d34cb657f90f92773d3fd64e3 (diff) | |
download | rails-6769293988c0d7733138a562aaa683375cbdcb78.tar.gz rails-6769293988c0d7733138a562aaa683375cbdcb78.tar.bz2 rails-6769293988c0d7733138a562aaa683375cbdcb78.zip |
connections must be checked in at the end of a thread
-rw-r--r-- | activerecord/CHANGELOG.md | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb | 18 | ||||
-rw-r--r-- | activerecord/test/cases/connection_pool_test.rb | 17 |
3 files changed, 7 insertions, 31 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 2c8ec3d4d1..c4feabfb5f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,8 @@ ## 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`: 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 ea738cb305..0d8efa60c2 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -155,7 +155,6 @@ module ActiveRecord # 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 @@ module ActiveRecord # 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 @@ connection. For example: ActiveRecord::Base.connection.close 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 diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb index d170a13b23..5b5bbc6500 100644 --- a/activerecord/test/cases/connection_pool_test.rb +++ b/activerecord/test/cases/connection_pool_test.rb @@ -35,27 +35,16 @@ module ActiveRecord 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= |