Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3096

Show
Ignore:
Timestamp:
11/19/05 10:55:11 (3 years ago)
Author:
bitsweat
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/base.rb

    r3089 r3096  
    248248    # to any of the specific Active Records. 
    249249    def self.connection 
    250       if allow_concurrency 
    251         retrieve_connection 
    252       else 
    253         @connection ||= retrieve_connection 
    254       end 
     250      retrieve_connection 
    255251    end 
    256252 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

    r3053 r3096  
    2020    # SchemaStatements#remove_column are very useful. 
    2121    class AbstractAdapter 
    22       include Quoting, DatabaseStatements, SchemaStatements 
     22      include Quoting, DatabaseStatements, SchemaStatements, ConnectionManagement 
    2323      @@row_even = true 
    24  
    25       @@reconnect_success = 0 
    26       @@reconnect_failure = 0 
    27       def self.reconnect_success_rate 
    28         (100.0 * @@reconnect_success / (@@reconnect_success + @@reconnect_failure)).to_i 
    29       end 
    3024 
    3125      def initialize(connection, logger = nil) #:nodoc: 
     
    7670          end 
    7771        rescue Exception => e 
     72          # Flag connection as possibly dirty; needs verification before use. 
     73          self.needs_verification! 
     74 
     75          # Log message and raise exception. 
    7876          message = "#{e.class.name}: #{e.message}: #{sql}" 
    79           unless reconnect_if_inactive! 
    80             message = "(reconnect failed) #{message}" 
    81           end 
    8277          log_info(message, name, 0) 
    8378          raise ActiveRecord::StatementInvalid, message 
     
    112107          end 
    113108        end 
    114  
    115       private 
    116         def reconnect_if_inactive! 
    117           if respond_to?(:active?) and respond_to?(:reconnect!) 
    118             reconnect! unless active? 
    119             if active? 
    120               @@reconnect_success += 1 
    121               @logger.info "#{adapter_name} automatically reconnected.  Success rate: #{self.class.reconnect_success_rate}%" if @logger 
    122               true 
    123             else 
    124               @@reconnect_failure += 1 
    125               @logger.warn "#{adapter_name} automatic reconnection failed.  Success rate: #{self.class.reconnect_success_rate}%" if @logger 
    126               false 
    127             end 
    128           else 
    129             @logger.warn "#{adapter_name} does not yet support automatic reconnection." if @logger 
    130           end 
    131         end 
    132109    end 
    133110  end 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb

    r2825 r3096  
    11module ActiveRecord 
     2  module ConnectionAdapters 
     3    module ConnectionManagement 
     4      # Check whether the connection should be checked for activity before use. 
     5      def needs_verification? 
     6        @needs_verification == true 
     7      end 
     8 
     9      # Flag the connection for an activity check before use. 
     10      def needs_verification! 
     11        @needs_verification = true 
     12      end 
     13 
     14      # If the connection is flagged for an activity check, check whether 
     15      # it is active and reconnect if not. 
     16      def verify! 
     17        if needs_verification? 
     18          reconnect! unless active? 
     19          @needs_verification = false 
     20        end 
     21        self 
     22      end 
     23    end 
     24  end 
     25 
    226  # The root class of all active record objects. 
    327  class Base 
     
    79103      until klass == ar_super 
    80104        if conn = active_connections[klass] 
     105          # Validate the active connection before returning it. 
     106          conn.verify! 
    81107          return conn 
    82108        elsif conn = @@defined_connections[klass] 
     109          # Activate this connection specification. 
    83110          klass.connection = conn 
    84111          return self.connection 
     
    126153      end 
    127154    end 
     155 
     156    # Mark active connections for verification on next retrieve_connection. 
     157    def self.mark_active_connections_for_verification! #:nodoc: 
     158      active_connections.values.each { |conn| conn.needs_verification! } 
     159    end 
    128160  end 
    129161end 
  • trunk/railties/lib/dispatcher.rb

    r2841 r3096  
    7474      def reset_after_dispatch 
    7575        reset_application! if Dependencies.load? 
     76        ActiveRecord::Base.mark_active_connections_for_verification! 
    7677        Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT) 
    7778      end