Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

r3181@asus: jeremy | 2005-11-19 02:52:24 -0800

 Mark connections for verification.  Retrieve connection verifies before returning a connection.  Verification tests whether the connection is marked then reconnects if the connection is inactive.  All active connections are marked for verification after a request is handled.  References #428.


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3096 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 81c5242f43cb45d97b2a56409f8b39b0dba75ac3 1 parent 77e0b79
@jeremy jeremy authored
View
6 activerecord/lib/active_record/base.rb
@@ -247,11 +247,7 @@ class Base
# also be used to "borrow" the connection to do database work unrelated
# to any of the specific Active Records.
def self.connection
- if allow_concurrency
- retrieve_connection
- else
- @connection ||= retrieve_connection
- end
+ retrieve_connection
end
# Returns the connection currently associated with the class. This can
View
32 activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
@@ -1,4 +1,28 @@
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:
@@ -78,8 +102,11 @@ def self.retrieve_connection #:nodoc:
ar_super = ActiveRecord::Base.superclass
until klass == ar_super
if conn = active_connections[klass]
+ # Validate the active connection before returning it.
+ conn.verify!
return conn
elsif conn = @@defined_connections[klass]
+ # Activate this connection specification.
klass.connection = conn
return self.connection
end
@@ -125,5 +152,10 @@ 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
View
33 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -19,15 +19,9 @@ module ConnectionAdapters # :nodoc:
# SchemaStatements#add_column, SchemaStatements#change_column and
# SchemaStatements#remove_column are very useful.
class AbstractAdapter
- include Quoting, DatabaseStatements, SchemaStatements
+ include Quoting, DatabaseStatements, SchemaStatements, ConnectionManagement
@@row_even = true
- @@reconnect_success = 0
- @@reconnect_failure = 0
- def self.reconnect_success_rate
- (100.0 * @@reconnect_success / (@@reconnect_success + @@reconnect_failure)).to_i
- end
-
def initialize(connection, logger = nil) #:nodoc:
@connection, @logger = connection, logger
@runtime = 0
@@ -75,10 +69,11 @@ 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}"
- unless reconnect_if_inactive!
- message = "(reconnect failed) #{message}"
- end
log_info(message, name, 0)
raise ActiveRecord::StatementInvalid, message
end
@@ -111,24 +106,6 @@ def format_log_entry(message, dump = nil)
"%s %s" % [message, dump]
end
end
-
- private
- def reconnect_if_inactive!
- if respond_to?(:active?) and respond_to?(:reconnect!)
- reconnect! unless active?
- if active?
- @@reconnect_success += 1
- @logger.info "#{adapter_name} automatically reconnected. Success rate: #{self.class.reconnect_success_rate}%" if @logger
- true
- else
- @@reconnect_failure += 1
- @logger.warn "#{adapter_name} automatic reconnection failed. Success rate: #{self.class.reconnect_success_rate}%" if @logger
- false
- end
- else
- @logger.warn "#{adapter_name} does not yet support automatic reconnection." if @logger
- end
- end
end
end
end
View
1  railties/lib/dispatcher.rb
@@ -73,6 +73,7 @@ def prepare_application
def reset_after_dispatch
reset_application! if Dependencies.load?
+ ActiveRecord::Base.mark_active_connections_for_verification!
Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT)
end

0 comments on commit 81c5242

Please sign in to comment.
Something went wrong with that request. Please try again.