diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 0ad70cd2d71239df2db26c18c078e953429b846f..26b8c52d59c2bc934bfa36d9239fc0f7e633753e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* 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] + * Correct reflected table name for singular associations. #1688 [court3nay@gmail.com] * Fixed optimistic locking with SQL Server #1660 [tom@popdog.net] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6ff0e4a9e0bd8d99451cc27b8d229927d72de7ed..b32843d06960923bd463ae4ab51ce0d934b9b3f1 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -289,6 +289,11 @@ def self.inherited(child) #:nodoc: # This is set to :local by default. cattr_accessor :default_timezone @@default_timezone = :local + + # Determines whether or not to use a connection for each thread, or a single shared connection for all threads. + # Defaults to true; Railties' WEBrick server sets this to false. + cattr_accessor :threaded_connections + @@threaded_connections = true class << self # Class methods # Find operates with three different retreval approaches: diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 75fd170269b7dbed7ad482d6fa27de1b0653e9e5..163925df42f8ec3e533901e62f39d118f8f610d4 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 @@ def self.establish_connection(spec = nil) 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 @@ def self.retrieve_connection #:nodoc: 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 @@ def self.retrieve_connection #:nodoc: 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 @@ def self.connected? 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 @@ def self.remove_connection(klass=self) 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. diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index 95b4d8c9151dcdc508cbec03e06b2b0ced4168db..c41567375f578b491cc7613ac6f6480e55b74345 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -51,8 +51,7 @@ def self.connection=(spec) QueryCache.new(self.send(spec.adapter_method, spec.config)) : self.send(spec.adapter_method, spec.config) - Thread.current['active_connections'] ||= {} - Thread.current['active_connections'][self] = conn + active_connections[self] = conn end end diff --git a/activerecord/test/threaded_connections_test.rb b/activerecord/test/threaded_connections_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..29c16b1a5cee146dba599f472c2f81af3de0edd2 --- /dev/null +++ b/activerecord/test/threaded_connections_test.rb @@ -0,0 +1,34 @@ +require 'abstract_unit' + +class ThreadedConnectionsTest < Test::Unit::TestCase + self.use_transactional_fixtures = false + + fixtures :topics + + def setup + @connection = ActiveRecord::Base.remove_connection + @connections = [] + end + + def gather_connections(use_threaded_connections) + ActiveRecord::Base.threaded_connections = use_threaded_connections + ActiveRecord::Base.establish_connection(@connection) + + 5.times do + Thread.new do + Topic.find :first + @connections << ActiveRecord::Base.active_connections.values.first + end.join + end + end + + def test_threaded_connections + gather_connections(true) + assert_equal @connections.uniq.length, 5 + end + + def test_unthreaded_connections + gather_connections(false) + assert_equal @connections.uniq.length, 1 + end +end diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 8f375a37742420f6da31a7b8d26e44bcfcb43169..8bba7464317223fc82bd810fe86983b527796072 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that each request with the WEBrick adapter would open a new database connection #1685 [Sam Stephenson] + * Added support for SQL Server in the database rake tasks #1652 [ken.barker@gmail.com] Note: osql and scptxfr may need to be installed on your development environment. This involves getting the .exes and a .rll (scptxfr) from a production SQL Server (not developer level SQL Server). Add their location to your Environment PATH and you are all set. * Added a VERSION parameter to the migrate task that allows you to do "rake migrate VERSION=34" to migrate to the 34th version traveling up or down depending on the current version diff --git a/railties/lib/webrick_server.rb b/railties/lib/webrick_server.rb index 71df5432e17d1d258e98c85c235c8bcf802d75f8..ba0541e7c692bd130d329ca918d004572d789203 100644 --- a/railties/lib/webrick_server.rb +++ b/railties/lib/webrick_server.rb @@ -8,6 +8,8 @@ ABSOLUTE_RAILS_ROOT = File.expand_path(RAILS_ROOT) +ActiveRecord::Base.threaded_connections = false + class CGI def stdinput @stdin || $stdin