提交 dc64887f 编写于 作者: J Jeremy Kemper

Connection cache to speed up retrieve_connection and get rid of dirty...

Connection cache to speed up retrieve_connection and get rid of dirty connection marking.  References #428.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3218 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 1dfa0bd3
......@@ -243,11 +243,17 @@ class Base
# 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
@@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
retrieve_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
......
module ActiveRecord
module ConnectionAdapters
module ConnectionManagement
# Check whether the connection should be checked for activity before use.
def needs_verification?
@needs_verification == true
end
# Flag the connection for an activity check before use.
def needs_verification!
@needs_verification = true
end
# If the connection is flagged for an activity check, check whether
# it is active and reconnect if not.
def verify!
if needs_verification?
reconnect! unless active?
@needs_verification = false
end
self
end
end
end
# The root class of all active record objects.
class Base
class ConnectionSpecification #:nodoc:
......@@ -102,8 +78,8 @@ def self.retrieve_connection #:nodoc:
ar_super = ActiveRecord::Base.superclass
until klass == ar_super
if conn = active_connections[klass.name]
# Validate the active connection before returning it.
conn.verify!
# Reconnect if the connection is inactive.
conn.reconnect! unless conn.active?
return conn
elsif conn = @@defined_connections[klass.name]
# Activate this connection specification.
......@@ -152,10 +128,5 @@ def self.connection=(spec)
establish_connection spec
end
end
# Mark active connections for verification on next retrieve_connection.
def self.mark_active_connections_for_verification! #:nodoc:
active_connections.values.each { |conn| conn.needs_verification! }
end
end
end
......@@ -19,7 +19,7 @@ module ConnectionAdapters # :nodoc:
# SchemaStatements#add_column, SchemaStatements#change_column and
# SchemaStatements#remove_column are very useful.
class AbstractAdapter
include Quoting, DatabaseStatements, SchemaStatements, ConnectionManagement
include Quoting, DatabaseStatements, SchemaStatements
@@row_even = true
def initialize(connection, logger = nil) #:nodoc:
......@@ -69,9 +69,6 @@ def log(sql, name)
nil
end
rescue Exception => e
# Flag connection as possibly dirty; needs verification before use.
self.needs_verification!
# Log message and raise exception.
message = "#{e.class.name}: #{e.message}: #{sql}"
log_info(message, name, 0)
......
......@@ -503,6 +503,8 @@ def setup_with_fixtures
alias_method :setup, :setup_with_fixtures
def teardown_with_fixtures
ActiveRecord::Base.clear_connection_cache!
ensure
# Rollback changes.
if use_transactional_fixtures?
ActiveRecord::Base.connection.rollback_db_transaction
......
......@@ -73,7 +73,7 @@ def prepare_application
def reset_after_dispatch
reset_application! if Dependencies.load?
ActiveRecord::Base.mark_active_connections_for_verification!
ActiveRecord::Base.clear_connection_cache!
Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册