class OTR::ActiveRecord::ConnectionManagement

Rack middleware that returns active db connections to the connection pool after a request completes.

Public Class Methods

new(app) click to toggle source
# File lib/otr-activerecord/middleware/connection_management.rb, line 7
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/otr-activerecord/middleware/connection_management.rb, line 11
def call(env)
  testing = env['rack.test'] == true

  resp = @app.call env
  resp[2] = ::Rack::BodyProxy.new resp[2] do
    ::ActiveRecord::Base.clear_active_connections! unless testing
  end
  resp

rescue Exception
  ::ActiveRecord::Base.clear_active_connections! unless testing
  raise
end