Class: Pools::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/pools/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pools = {}) ⇒ Handler

Returns a new instance of Handler.



9
10
11
# File 'lib/pools/handler.rb', line 9

def initialize(pools = {})
  @pools = pools
end

Instance Attribute Details

#poolsObject (readonly)

Returns the value of attribute pools.



7
8
9
# File 'lib/pools/handler.rb', line 7

def pools
  @pools
end

Instance Method Details

#add(pool, name = nil) ⇒ Object

Add a new connection pool to the mix



14
15
16
17
18
# File 'lib/pools/handler.rb', line 14

def add(pool, name = nil)
  key = name || pool.object_id
  raise(%Q(Pool "#{name}" already exists)) if @pools[name]
  @pools[name] = pool
end

#clear_active_connections!Object

Returns any connections in use by the current thread back to the pool, and also returns connections to the pool cached by threads that are no longer alive.



23
24
25
# File 'lib/pools/handler.rb', line 23

def clear_active_connections!
  @pools.each_value {|pool| pool.release_connection }
end

#clear_all_connections!Object



27
28
29
# File 'lib/pools/handler.rb', line 27

def clear_all_connections!
  @pools.each_value {|pool| pool.disconnect! }
end

#connected?(name) ⇒ Boolean

Returns true if a connection that’s accessible to this class has already been opened.

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/pools/handler.rb', line 38

def connected?(name)
  conn = retrieve_connection_pool(name)
  conn && conn.connected?
end

#remove_connection(name) ⇒ Object

Remove the connection for this class. This will close the active connection and the defined connection (if they exist). The result can be used as an argument for establish_connection, for easily re-establishing the connection.



47
48
49
50
51
52
53
# File 'lib/pools/handler.rb', line 47

def remove_connection(name)
  pool = retrieve_connection_pool(name)
  return nil unless pool

  @pools.delete_if { |key, value| value == pool }
  pool.disconnect!
end

#retrieve_connection_pool(name) ⇒ Object



55
56
57
58
# File 'lib/pools/handler.rb', line 55

def retrieve_connection_pool(name)
  pool = @pools[name]
  return pool if pool
end

#verify_active_connections!Object

Verify active connections.



32
33
34
# File 'lib/pools/handler.rb', line 32

def verify_active_connections! #:nodoc:
  @pools.each_value {|pool| pool.verify_active_connections! }
end