1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
module OTR
module ActiveRecord
#
# Rack middleware that returns active db connections to the connection pool after a request completes.
#
class ConnectionManagement
def initialize(app)
@app = app
end
def call(env)
testing = env['rack.test'] == true
resp = @app.call env
resp[2] = ::Rack::BodyProxy.new resp[2] do
::ActiveRecord::Base.connection_handler.clear_active_connections! unless testing
end
resp
rescue => e
::ActiveRecord::Base.connection_handler.clear_active_connections! unless testing
raise e
end
end
end
end
|