Sha256: 6352b73182a5043e2c6b3f174e46231d6cc14128c39a021d07a9408c40da6ed1

Contents?: true

Size: 1005 Bytes

Versions: 2

Compression:

Stored size: 1005 Bytes

Contents

require 'fresh_connection/abstract_connection_manager'

module FreshConnection
  class ConnectionManager < AbstractConnectionManager
    def slave_connection
      synchronize do
        slave_connections[current_thread_id] ||= new_connection
      end
    end

    def put_aside!
      synchronize do
        if c = slave_connections.delete(current_thread_id)
          c.disconnect! rescue nil
        end
      end
    end

    def recovery(failure_connection, exception)
      if recoverable? && slave_down_message?(exception.message)
        put_aside!
        true
      else
        false
      end
    end

    def recoverable?
      true
    end

    private

    def slave_connections
      @slave_connections ||= {}
    end

    def new_connection
      ActiveRecord::Base.send("mysql2_connection", spec)
    end

    def spec
      @spec ||= get_spec
    end

    def get_spec
      ret = ActiveRecord::Base.configurations[Rails.env]
      ret.merge(ret[@slave_group] || {})
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fresh_connection-0.2.1 lib/fresh_connection/connection_manager.rb
fresh_connection-0.2.0 lib/fresh_connection/connection_manager.rb