diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-03-13 17:28:55 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-03-13 17:28:55 +0000 |
commit | 25fb2db409f30437d901bf644b7cdea39ce64fdb (patch) | |
tree | 49eb45a58db31704eb2574452ac83b6ebe637189 /activerecord/lib/active_record/connection_adapters/abstract | |
parent | 8ff44631939fd9220f03f9b18a687cbd040220e3 (diff) | |
download | rails-25fb2db409f30437d901bf644b7cdea39ce64fdb.tar.gz rails-25fb2db409f30437d901bf644b7cdea39ce64fdb.tar.bz2 rails-25fb2db409f30437d901bf644b7cdea39ce64fdb.zip |
Dynamically set allow_concurrency. Closes #4044.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3862 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb | 66 |
1 files changed, 47 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb index fe581a14b5..8b70b2f047 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb @@ -22,15 +22,35 @@ module ActiveRecord class << self # Retrieve the connection cache. - def active_connections - if @@allow_concurrency - @@active_connections[Thread.current.object_id] ||= {} - else - @@active_connections + def thread_safe_active_connections #:nodoc: + @@active_connections[Thread.current.object_id] ||= {} + end + + def single_threaded_active_connections #:nodoc: + @@active_connections + end + + # pick up the right active_connection method from @@allow_concurrency + if @@allow_concurrency + alias_method :active_connections, :thread_safe_active_connections + else + alias_method :active_connections, :single_threaded_active_connections + end + + # set concurrency support flag (not thread safe, like most of the methods in this file) + def allow_concurrency=(threaded) + logger.debug "allow_concurrency=#{threaded}" if logger + return if @@allow_concurrency == threaded + clear_all_cached_connections! + @@allow_concurrency = threaded + method_prefix = threaded ? "thread_safe" : "single_threaded" + sing = (class << self; self; end) + [:active_connections, :scoped_methods].each do |method| + sing.send(:alias_method, method, "#{method_prefix}_#{method}") end + log_connections if logger end - - @active_connection_name = nil + def active_connection_name @active_connection_name ||= if active_connections[name] || @@defined_connections[name] @@ -62,15 +82,19 @@ module ActiveRecord # Clears the cache which maps classes to connections. def clear_active_connections! - clear_cache!(@@active_connections) + clear_cache!(@@active_connections) do |name, conn| + conn.disconnect! + end end # Verify active connections. def verify_active_connections! - remove_stale_cached_threads!(@@active_connections) do |name, conn| - conn.disconnect! + if @@allow_concurrency + remove_stale_cached_threads!(@@active_connections) do |name, conn| + conn.disconnect! + end end - + active_connections.each_value do |connection| connection.verify!(@@verification_timeout) end @@ -106,6 +130,18 @@ module ActiveRecord clear_cache!(cache, thread_id, &block) end end + + def clear_all_cached_connections! + if @@allow_concurrency + @@active_connections.each_value do |connection_hash_for_thread| + connection_hash_for_thread.each_value {|conn| conn.disconnect! } + connection_hash_for_thread.clear + end + else + @@active_connections.each_value {|conn| conn.disconnect! } + end + @@active_connections.clear + end end # Returns the connection currently associated with the class. This can @@ -167,14 +203,6 @@ module ActiveRecord end end - def self.active_connections #:nodoc: - if @@allow_concurrency - Thread.current['active_connections'] ||= {} - else - @@active_connections ||= {} - end - end - # Locate the connection of the nearest super class. This can be an # active or defined connections: if it is the latter, it will be # opened and set as the active connection for the class it was defined |