1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Clear the connection cache entry when a new connection is established on the same class.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3222 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2005-12-05 21:48:23 +00:00
parent b13573ed37
commit 3ed6675c68
2 changed files with 24 additions and 21 deletions

View file

@ -243,26 +243,6 @@ module ActiveRecord #:nodoc:
# on to any new database connections made and which can be retrieved on both a class and instance level by calling +logger+. # on to any new database connections made and which can be retrieved on both a class and instance level by calling +logger+.
cattr_accessor :logger cattr_accessor :logger
@@connection_cache = Hash.new { |h, k| h[k] = Hash.new }
# Returns the connection currently associated with the class. This can
# also be used to "borrow" the connection to do database work unrelated
# to any of the specific Active Records.
def self.connection
@@connection_cache[Thread.current.object_id][name] ||= retrieve_connection
end
def self.clear_connection_cache!
@@connection_cache.clear
end
# Returns the connection currently associated with the class. This can
# also be used to "borrow" the connection to do database work that isn't
# easily done without going straight to SQL.
def connection
self.class.connection
end
def self.inherited(child) #:nodoc: def self.inherited(child) #:nodoc:
@@subclasses[self] ||= [] @@subclasses[self] ||= []
@@subclasses[self] << child @@subclasses[self] << child

View file

@ -11,6 +11,28 @@ module ActiveRecord
# The class -> [adapter_method, config] map # The class -> [adapter_method, config] map
@@defined_connections = {} @@defined_connections = {}
# The class -> thread id -> adapter cache.
@@connection_cache = Hash.new { |h, k| h[k] = Hash.new }
# Returns the connection currently associated with the class. This can
# also be used to "borrow" the connection to do database work unrelated
# to any of the specific Active Records.
def self.connection
@@connection_cache[Thread.current.object_id][name] ||= retrieve_connection
end
# Clears the cache which maps classes to connections.
def self.clear_connection_cache!
@@connection_cache.clear
end
# Returns the connection currently associated with the class. This can
# also be used to "borrow" the connection to do database work that isn't
# easily done without going straight to SQL.
def connection
self.class.connection
end
# Establishes the connection to the database. Accepts a hash as input where # Establishes the connection to the database. Accepts a hash as input where
# the :adapter key must be specified with the name of a database adapter (in lower-case) # the :adapter key must be specified with the name of a database adapter (in lower-case)
# example for regular databases (MySQL, Postgresql, etc): # example for regular databases (MySQL, Postgresql, etc):
@ -111,7 +133,8 @@ module ActiveRecord
def self.remove_connection(klass=self) def self.remove_connection(klass=self)
conn = @@defined_connections[klass.name] conn = @@defined_connections[klass.name]
@@defined_connections.delete(klass.name) @@defined_connections.delete(klass.name)
active_connections[klass.name] = nil @@connection_cache[Thread.current.object_id].delete(klass.name)
active_connections.delete(klass.name)
@connection = nil @connection = nil
conn.config if conn conn.config if conn
end end