Class: MultiConnection::ConnectionAdapters::ConnectionHandler

Inherits:
ActiveRecord::ConnectionAdapters::ConnectionHandler
  • Object
show all
Defined in:
lib/multi_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnectionHandler

Returns a new instance of ConnectionHandler.



60
61
62
# File 'lib/multi_connection.rb', line 60

def initialize
  @spec_to_pool = ThreadSafe::Cache.new(:initial_capacity => 2)
end

Instance Attribute Details

#specObject

Returns the value of attribute spec.



58
59
60
# File 'lib/multi_connection.rb', line 58

def spec
  @spec
end

Instance Method Details

#connection_pool_listObject



64
65
66
# File 'lib/multi_connection.rb', line 64

def connection_pool_list
  @spec_to_pool.values.compact
end

#establish_connection(owner, spec) ⇒ Object



68
69
70
71
# File 'lib/multi_connection.rb', line 68

def establish_connection(owner, spec)
  @spec_to_pool[self.spec] =
    ::ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
end

#remove_connection(spec) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/multi_connection.rb', line 73

def remove_connection(spec)
  if pool = @spec_to_pool[spec]
    pool.automatic_reconnect = false
    pool.disconnect!
    pool.spec.config
  end
end

#retrieve_connection_pool(klass = nil) ⇒ Object



81
82
83
84
85
# File 'lib/multi_connection.rb', line 81

def retrieve_connection_pool(klass=nil)
  # Base.establish_connection will resolve the spec for us 
  # and call our #establish_connection method
  @spec_to_pool[spec] || ::ActiveRecord::Base.establish_connection(spec)
end