Class: ActiveRecord::ConnectionAdapters::RefreshConnectionManagement

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-refresh_connection/active_record/connection_adapters/refresh_connection_management.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{max_requests: 1}

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RefreshConnectionManagement

Returns a new instance of RefreshConnectionManagement.


6
7
8
9
10
11
12
# File 'lib/activerecord-refresh_connection/active_record/connection_adapters/refresh_connection_management.rb', line 6

def initialize(app, options = {})
  @app = app
  @options = DEFAULT_OPTIONS.merge(options)
  @mutex = Mutex.new

  reset_remain_count
end

Instance Method Details

#call(env) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/activerecord-refresh_connection/active_record/connection_adapters/refresh_connection_management.rb', line 14

def call(env)
  testing = env.key?('rack.test')

  response = @app.call(env)

  clear_connections = should_clear_connections? && !testing

  response[2] = ::Rack::BodyProxy.new(response[2]) do
    # disconnect all connections on the connection pool
    ActiveRecord::Base.clear_all_connections! if clear_connections
  end

  response
rescue Exception
  ActiveRecord::Base.clear_all_connections! if clear_connections
  raise
end