Class: Lore::Connection_Pool

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

Constant Summary collapse

@@pool =
Hash.new

Class Method Summary collapse

Class Method Details

.get_connection(db_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lore/connection.rb', line 58

def self.get_connection(db_name)
  
  db_name = db_name.intern unless db_name.instance_of? Symbol
  
  # If requested connection is not in pool yet: 
  if !@@pool.has_key? db_name then
    # Try to establish connection
    begin
      connection = PGconn.connect(Lore.pg_server, Lore.pg_port, '', '', db_name.to_s, Lore.user_for(db_name), Lore.pass_for(db_name.to_s))
      connection.exec(Lore.on_connect_commands)
      @@pool[db_name] = connection
    # Handle errors
    rescue PGError => pge
      raise Lore::Connection_Error.new("Could not establish connection to database '#{db_name.to_s}': " << pge.message)
    rescue ::Exception => excep
      raise Lore::Connection_Error.new("Could not establish connection to database '#{db_name.to_s}': " << excep.message)
    end
  end

  # Return requested connection
  return @@pool[db_name]

end