diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-10 04:54:34 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-10 04:54:34 +0000 |
commit | 60499774c2a8ecd9d87b140ffbe24634559c3fcd (patch) | |
tree | e4e2a348e4dae4d107f020f9c21664a5e169b92c /activerecord/lib/active_record/connection_adapters | |
parent | e008bfd2d8e8e19eb94ddb49121820effe93d7f4 (diff) | |
download | rails-60499774c2a8ecd9d87b140ffbe24634559c3fcd.tar.gz rails-60499774c2a8ecd9d87b140ffbe24634559c3fcd.tar.bz2 rails-60499774c2a8ecd9d87b140ffbe24634559c3fcd.zip |
Fixed that each request with the WEBrick adapter would open a new database connection #1685 [Sam Stephenson]. Added ActiveRecord::Base.threaded_connections flag to turn off 1-connection per thread (required for thread safety). By default it's on, but WEBrick in Rails need it off #1685 [Sam Stephenson]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1792 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 75fd170269..163925df42 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -86,6 +86,14 @@ module ActiveRecord end end + def self.active_connections #:nodoc: + if threaded_connections + 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 @@ -94,7 +102,7 @@ module ActiveRecord klass = self ar_super = ActiveRecord::Base.superclass until klass == ar_super - if conn = (Thread.current['active_connections'] ||= {})[klass] + if conn = active_connections[klass] return conn elsif conn = @@defined_connections[klass] klass.connection = conn @@ -109,7 +117,7 @@ module ActiveRecord def self.connected? klass = self until klass == ActiveRecord::Base.superclass - if Thread.current['active_connections'].is_a?(Hash) && Thread.current['active_connections'][klass] + if active_connections[klass] return true else klass = klass.superclass @@ -125,8 +133,7 @@ module ActiveRecord def self.remove_connection(klass=self) conn = @@defined_connections[klass] @@defined_connections.delete(klass) - Thread.current['active_connections'] ||= {} - Thread.current['active_connections'][klass] = nil + active_connections[klass] = nil conn.config if conn end @@ -134,8 +141,7 @@ module ActiveRecord def self.connection=(spec) raise ConnectionNotEstablished unless spec conn = self.send(spec.adapter_method, spec.config) - Thread.current['active_connections'] ||= {} - Thread.current['active_connections'][self] = conn + active_connections[self] = conn end # Converts all strings in a hash to symbols. |