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
# File 'lib/pools/handler.rb', line 14

def add(pool, name = nil)
  @pools[name || pool.object_id] = 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.



21
22
23
# File 'lib/pools/handler.rb', line 21

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

#clear_all_connections!Object



25
26
27
# File 'lib/pools/handler.rb', line 25

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)


36
37
38
39
# File 'lib/pools/handler.rb', line 36

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.



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

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



53
54
55
56
# File 'lib/pools/handler.rb', line 53

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

#verify_active_connections!Object

Verify active connections.



30
31
32
# File 'lib/pools/handler.rb', line 30

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