Class: TingYun::Agent::Database::ConnectionManager

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#close_connectionsObject

Closes all the connections in the internal connection cache



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/ting_yun/agent/database.rb', line 253

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

  @connections = {}
end

#get_connection(config, &connector) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ting_yun/agent/database.rb', line 237

def get_connection(config, &connector)
  @connections ||= {}

  connection = @connections[config]

  return connection if connection

  begin
    @connections[config] = connector.call(config)
  rescue => e
    ::TingYun::Agent.logger.error("Caught exception trying to get connection to DB for explain.", e)
    nil
  end
end