From 72d959d9b5255a449a554a1f011386d3c7a568cf Mon Sep 17 00:00:00 2001 From: Nick Sieger Date: Sat, 7 Jun 2008 16:30:56 -0500 Subject: Split connection handler into single- and multiple-thread versions. --- .../abstract/connection_specification.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb') 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 2dc3201208..0ebb191381 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb @@ -14,12 +14,24 @@ module ActiveRecord # The connection handler cattr_accessor :connection_handler, :instance_writer => false - @@connection_handler = ConnectionAdapters::ConnectionHandler.new + @@connection_handler = ConnectionAdapters::SingleThreadConnectionHandler.new - # Turning on allow_concurrency basically switches a null mutex for a real one, so that - # multi-threaded access of the connection pools hash is synchronized. + # Turning on allow_concurrency changes the single threaded connection handler + # for a multiple threaded one, so that multi-threaded access of the + # connection pools is synchronized. def self.allow_concurrency=(flag) - @@allow_concurrency = flag + if @@allow_concurrency != flag + @@allow_concurrency = flag + # When switching connection handlers, preserve the existing pools so that + # #establish_connection doesn't need to be called again. + if @@allow_concurrency + self.connection_handler = ConnectionAdapters::MultipleThreadConnectionHandler.new( + self.connection_handler.connection_pools) + else + self.connection_handler = ConnectionAdapters::SingleThreadConnectionHandler.new( + self.connection_handler.connection_pools) + end + end end # Returns the connection currently associated with the class. This can @@ -112,7 +124,7 @@ module ActiveRecord connection_handler.connected?(self) end - def remove_connection(klass=self) + def remove_connection(klass = self) connection_handler.remove_connection(klass) end -- cgit v1.2.3