Class: DbBrowser::ConnectionMan

Inherits:
Object
  • Object
show all
Defined in:
lib/dbbrowser/connection.rb

Overview

connection manager

Defined Under Namespace

Classes: AbstractModel, Column, Connection, Result, Table

Constant Summary collapse

CONNECTS =

get connection names

def connection_names
  ActiveRecord::Base.configurations.keys
end
{}

Instance Method Summary collapse

Instance Method Details

#connection_for(key) ⇒ Object

cache connections



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dbbrowser/connection.rb', line 12

def connection_for( key )
  # cache connections - needed? why? why not??

  #  hack: for now only use cached connection if still active
  #   if not; get a new one to avoid connection closed errors in rails
  con = CONNECTS[ key ]
  if con
    puts "[ConnectionMan] cached connection found; con.active? #{con.active?}"
    unless con.active?
      puts "[ConnectionMan] *** reset cached connection (reason: connection stale/closed/not active)"
      con = CONNECTS[ key ] = nil
    end
  end

  if con.nil?
    con = CONNECTS[ key ] =  AbstractModel.connection_for( key )
  end

  # note: make sure connection is active?
  #  use verify!  - will try active? followed by reconnect!
  # - todo: check ourselves if active? - why? why not??
  #  -- not working w/ rails - after verify! still getting error w/ closed connection
  # -- con.verify!

  # wrap ActiveRecord connection in our own connection class
  Connection.new( con, key )
end