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

Changeset 3218

Show
Ignore:
Timestamp:
12/04/05 05:56:49 (3 years ago)
Author:
bitsweat
Message:

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

Files:

Legend:

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

    r3213 r3218  
    244244    cattr_accessor :logger 
    245245 
     246    @@connection_cache = Hash.new { |h, k| h[k] = Hash.new } 
     247 
    246248    # Returns the connection currently associated with the class. This can 
    247249    # also be used to "borrow" the connection to do database work unrelated 
    248250    # to any of the specific Active Records. 
    249251    def self.connection 
    250       retrieve_connection 
     252      @@connection_cache[Thread.current.object_id][name] ||= retrieve_connection 
     253    end 
     254 
     255    def self.clear_connection_cache! 
     256      @@connection_cache.clear 
    251257    end 
    252258 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

    r3096 r3218  
    2020    # SchemaStatements#remove_column are very useful. 
    2121    class AbstractAdapter 
    22       include Quoting, DatabaseStatements, SchemaStatements, ConnectionManagement 
     22      include Quoting, DatabaseStatements, SchemaStatements 
    2323      @@row_even = true 
    2424 
     
    7070          end 
    7171        rescue Exception => e 
    72           # Flag connection as possibly dirty; needs verification before use. 
    73           self.needs_verification! 
    74  
    7572          # Log message and raise exception. 
    7673          message = "#{e.class.name}: #{e.message}: #{sql}" 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb

    r3182 r3218  
    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  
    262  # The root class of all active record objects. 
    273  class Base 
     
    10379      until klass == ar_super 
    10480        if conn = active_connections[klass.name] 
    105           # Validate the active connection before returning it
    106           conn.verify! 
     81          # Reconnect if the connection is inactive
     82          conn.reconnect! unless conn.active? 
    10783          return conn 
    10884        elsif conn = @@defined_connections[klass.name] 
     
    153129      end 
    154130    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 
    160131  end 
    161132end 
  • trunk/activerecord/lib/active_record/fixtures.rb

    r2862 r3218  
    504504 
    505505      def teardown_with_fixtures 
     506        ActiveRecord::Base.clear_connection_cache! 
     507      ensure 
    506508        # Rollback changes. 
    507509        if use_transactional_fixtures? 
  • trunk/railties/lib/dispatcher.rb

    r3096 r3218  
    7474      def reset_after_dispatch 
    7575        reset_application! if Dependencies.load? 
    76         ActiveRecord::Base.mark_active_connections_for_verification
     76        ActiveRecord::Base.clear_connection_cache
    7777        Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT) 
    7878      end