Module: MultiConnection::ConnectionHandling

Defined in:
lib/multi_connection.rb

Instance Method Summary collapse

Instance Method Details

#clear_active_connections!Object



6
7
8
9
# 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!Object



16
17
18
19
# 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!Object



11
12
13
14
# 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) ⇒ Object

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



38
39
40
41
42
43
44
45
46
# 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