File: connection_management.rb

package info (click to toggle)
ruby-otr-activerecord 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 356 kB
  • sloc: ruby: 561; sh: 133; makefile: 6
file content (26 lines) | stat: -rw-r--r-- 651 bytes parent folder | download
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