module MultiConnection::ConnectionHandling

Public Instance Methods

clear_active_connections!() click to toggle source
# File lib/multi_connection.rb, line 6
def clear_active_connections!
  connection_handler.clear_active_connections!
  ghost_connection_handler.clear_active_connections!
end
clear_all_connections!() click to toggle source
# File lib/multi_connection.rb, line 16
def clear_all_connections!
  connection_handler.clear_all_connections!
  ghost_connection_handler.clear_all_connections!
end
clear_reloadable_connections!() click to toggle source
# File lib/multi_connection.rb, line 11
def clear_reloadable_connections!
  connection_handler.clear_reloadable_connections!
  ghost_connection_handler.clear_reloadable_connections!
end
switch_to(spec) { || ... } click to toggle source

Connect to another database. And restore the previous connection at the end of the block.

spec - a symbol or string

Note, switch_to will change the connection handler which means all subsequent queries in that block will be sent to the new database.

ActiveRecord::Base.switch_to(:another_db) {
  # query sent to another_db
}

This is thread safe since connection_handler is local to current thread according to ActiveSupport::PerThreadRegistry.

Yield a block

# File lib/multi_connection.rb, line 38
def switch_to(spec)
  old_handler = connection_handler
  self.connection_handler = ghost_connection_handler
  self.connection_handler.spec = spec
  yield
ensure
  self.connection_handler.spec = nil
  self.connection_handler = old_handler
end

Private Instance Methods

ghost_connection_handler() click to toggle source
# File lib/multi_connection.rb, line 50
def ghost_connection_handler
  @ghost_connection_handler ||=
    ::MultiConnection::ConnectionAdapters::ConnectionHandler.new
end