Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def initialize(app, options = {})
@options = DEFAULT_OPTIONS.merge(options)
@mutex = Mutex.new

resolve_clear_connections
reset_remain_count
end

Expand All @@ -30,6 +31,41 @@ def call(env)
private

def clear_connections
__send__(@clear_connections)
end

def resolve_clear_connections
ar_version = ActiveRecord.gem_version.to_s

@clear_connections =
if ar_version >= "6.1"
:clear_multi_db_connections
elsif ar_version >= "6.0"
:clear_legacy_compatible_connections
else
:clear_legacy_connections
end
end

def clear_multi_db_connections
if should_clear_all_connections?
ActiveRecord::Base.connection_handler.all_connection_pools.each(&:disconnect!)
else
ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct for legacy_connection_handling = false but is not always correct for legacy_connection_handling = true.

pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
end
end
end

def clear_legacy_compatible_connections
if should_clear_all_connections?
ActiveRecord::Base.connection_handlers.each_value(&:clear_all_connections!)
else
ActiveRecord::Base.connection_handlers.each_value(&:clear_active_connections!)
end
end

def clear_legacy_connections
if should_clear_all_connections?
ActiveRecord::Base.clear_all_connections!
else
Expand Down