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

Changeset 3001

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

r3042@asus: jeremy | 2005-11-13 01:51:08 -0800
MySQL active? and reconnect! References #428.
r3043@asus: jeremy | 2005-11-13 01:58:28 -0800
SQLite active? and reconnect! References #428.
r3044@asus: jeremy | 2005-11-13 02:02:27 -0800
Update CHANGELOG with admonishment regarding avoidance of the log method.

Files:

Legend:

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

    r3000 r3001  
    11*SVN* 
    22 
    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] 
     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, MySQL, and SQLite.  Make certain that all statements which may need reconnection are performed within a logged block: for example, this means no avoiding log(sql, name) { } if @logger.nil?  [Jeremy Kemper] 
    44 
    55* Much faster Oracle column reflection.  #2848 [Michael Schoen <schoenm@earthlink.net>] 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

    r3000 r3001  
    2323      @@row_even = true 
    2424 
     25      @@reconnect_success = 0 
     26      @@reconnect_failure = 0 
     27      def self.reconnect_success_rate 
     28        @@reconnect_success.to_f / (@@reconnect_success + @@reconnect_failure) 
     29      end 
     30 
    2531      def initialize(connection, logger = nil) #:nodoc: 
    2632        @connection, @logger = connection, logger 
     
    4147 
    4248      def reset_runtime #:nodoc: 
    43         rt = @runtime 
    44         @runtime = 0 
    45         return rt 
     49        rt, @runtime = @runtime, 0 
     50        rt 
    4651      end 
    4752 
     
    101106          if respond_to?(:active?) and respond_to?(:reconnect!) 
    102107            reconnect! unless active? 
    103             raise ActiveRecord::ConnectionFailed unless active? 
     108            if active? 
     109              @@reconnect_success += 1 
     110              @logger.info "#{adapter_name} automatically reconnected.  Success rate: #{'%.2f' % self.class.reconnect_success_rate}%" if @logger 
     111            else 
     112              @@reconnect_failure += 1 
     113              @logger.warn "#{adapter_name} automatic reconnection failed.  Success rate: #{'%.2f' % self.class.reconnect_success_rate}%" if @logger 
     114              raise ActiveRecord::ConnectionFailed 
     115            end 
    104116          else 
    105117            @logger.warn "#{adapter_name} does not yet support automatic reconnection." if @logger 
  • trunk/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    r2947 r3001  
    137137 
    138138 
     139      # CONNECTION MANAGEMENT ==================================== 
     140 
     141      def active? 
     142        @connection.stat if @connection.respond_to?(:stat) 
     143        true 
     144      rescue Mysql::Error 
     145        false 
     146      end 
     147 
     148      def reconnect! 
     149        if @connection.respond_to?(:ping) 
     150          @connection.ping 
     151        else 
     152          @connection.close rescue nil 
     153          @connection.real_connect(*@connection_options) 
     154        end 
     155      end 
     156 
     157 
    139158      # DATABASE STATEMENTS ====================================== 
    140159 
     
    149168 
    150169      def execute(sql, name = nil, retries = 2) #:nodoc: 
    151         unless @logger 
    152           @connection.query(sql) 
    153         else 
    154           log(sql, name) { @connection.query(sql) } 
    155         end 
     170        log(sql, name) { @connection.query(sql) } 
    156171      rescue ActiveRecord::StatementInvalid => exception 
    157         if LOST_CONNECTION_ERROR_MESSAGES.any? { |msg| exception.message.split(":").first =~ /^#{msg}/ } 
    158           @connection.real_connect(*@connection_options) 
    159           unless @logger 
    160             @connection.query(sql) 
    161           else 
    162             @logger.info "Retrying invalid statement with reopened connection" 
    163             log(sql, name) { @connection.query(sql) } 
    164           end 
    165         elsif exception.message.split(":").first =~ /Packets out of order/ 
    166           raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem update mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information." 
     172        if exception.message.split(":").first =~ /Packets out of order/ 
     173          raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information.  If you're on Windows, use the Instant Rails installer to get the updated mysql bindings." 
    167174        else 
    168175          raise 
  • trunk/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb

    r2962 r3001  
    128128 
    129129 
     130      # CONNECTION MANAGEMENT ==================================== 
     131 
     132      def active? 
     133        # TODO: SQLite is an embedded db, it doesn't lose connections, 
     134        # but perhaps some of its exceptions merit a retry, such as 
     135        # LockedException. 
     136        true 
     137      end 
     138 
     139      def reconnect! 
     140      end 
     141 
     142 
    130143      # DATABASE STATEMENTS ====================================== 
    131144