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

Changeset 3000

Show
Ignore:
Timestamp:
11/13/05 08:23:07 (3 years ago)
Author:
bitsweat
Message:

r3032@asus: jeremy | 2005-11-12 23:16:52 -0800
Ticket 428 - stale connections
r3040@asus: jeremy | 2005-11-13 00:22:29 -0800
When AbstractAdapter#log rescues an exception, attempt to detect and reconnect to an inactive database connection. Connection adapter must respond to the active? and reconnect! instance methods. Initial support for PostgreSQL. References #428.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r2996 r3000  
    11*SVN* 
     2 
     3* When AbstractAdapter#log rescues an exception, attempt to detect and reconnect to an inactive database connection.  Connection adapter must respond to the active? and reconnect! instance methods.  Initial support for PostgreSQL.  [Jeremy Kemper] 
    24 
    35* Much faster Oracle column reflection.  #2848 [Michael Schoen <schoenm@earthlink.net>] 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

    r2761 r3000  
    4848      protected   
    4949        def log(sql, name) 
    50           begin 
    51             if block_given? 
    52               if @logger and @logger.level <= Logger::INFO 
    53                 result = nil 
    54                 seconds = Benchmark.realtime { result = yield } 
    55                 @runtime += seconds 
    56                 log_info(sql, name, seconds) 
    57                 result 
    58               else 
    59                 yield 
    60               end 
     50          if block_given? 
     51            if @logger and @logger.level <= Logger::INFO 
     52              result = nil 
     53              seconds = Benchmark.realtime { result = yield } 
     54              @runtime += seconds 
     55              log_info(sql, name, seconds) 
     56              result 
    6157            else 
    62               log_info(sql, name, 0) 
    63               nil 
     58              yield 
    6459            end 
    65           rescue Exception =>
    66             log_info("#{e.message}: #{sql}", name, 0) 
    67             raise ActiveRecord::StatementInvalid, "#{e.message}: #{sql}" 
     60          els
     61            log_info(sql, name, 0) 
     62            nil 
    6863          end 
     64        rescue Exception => e 
     65          log_info("#{e.message}: #{sql}", name, 0) 
     66          reconnect_if_inactive! 
     67          raise ActiveRecord::StatementInvalid, "#{e.message}: #{sql}" 
    6968        end 
    7069 
     
    9796          end 
    9897        end 
     98 
     99      private 
     100        def reconnect_if_inactive! 
     101          if respond_to?(:active?) and respond_to?(:reconnect!) 
     102            reconnect! unless active? 
     103            raise ActiveRecord::ConnectionFailed unless active? 
     104          else 
     105            @logger.warn "#{adapter_name} does not yet support automatic reconnection." if @logger 
     106          end 
     107        end 
    99108    end 
    100109  end 
  • trunk/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

    r2998 r3000  
    5151      def adapter_name 
    5252        'PostgreSQL' 
     53      end 
     54 
     55      # Is this connection alive and ready for queries? 
     56      def active? 
     57        # TODO: postgres-pr doesn't have PGconn#status. 
     58        if @connection.respond_to?(:status) 
     59          @connection.status != PGconn::CONNECTION_BAD 
     60        else 
     61          true 
     62        end 
     63      end 
     64 
     65      # Close then reopen the connection. 
     66      def reconnect! 
     67        # TODO: postgres-pr doesn't have PGconn#reset. 
     68        if @connection.respond_to?(:reset) 
     69          @connection.reset 
     70        end 
    5371      end 
    5472