Class: NewRelic::Agent::Database::ConnectionManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/new_relic/agent/database.rb

Instance Method Summary collapse

Instance Method Details

#close_connectionsObject

Closes all the connections in the internal connection cache



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/new_relic/agent/database.rb', line 140

def close_connections
  @connections ||= {}
  @connections.values.each do |connection|
    begin
      connection.disconnect!
    rescue
    end
  end
  
  @connections = {}
end

#get_connection(config) ⇒ Object

Returns a cached connection for a given ActiveRecord configuration - these are stored or reopened as needed, and if we cannot get one, we ignore it and move on without explaining the sql



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/new_relic/agent/database.rb', line 122

def get_connection(config)
  @connections ||= {}
  
  connection = @connections[config]
  
  return connection if connection
  
  begin
    connection = ActiveRecord::Base.send("#{config[:adapter]}_connection", config)
    @connections[config] = connection
  rescue => e
    NewRelic::Agent.agent.log.error("Caught exception #{e} trying to get connection to DB for explain. Control: #{config}")
    NewRelic::Agent.agent.log.error(e.backtrace.join("\n"))
    nil
  end
end