Skip to content

Support Rails 6 multiple database connections. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
@@ -8,6 +8,7 @@ def initialize(app, options = {})
@options = DEFAULT_OPTIONS.merge(options)
@mutex = Mutex.new

resolve_clear_connections
reset_remain_count
end

@@ -30,6 +31,61 @@ 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"
if ActiveRecord::Base.legacy_connection_handling
:clear_legacy_compatible_connections
else
:clear_multi_db_connections
end
elsif ar_version >= "6.0"
:clear_ar_6_0_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 do |handler|
handler.connection_pool_list.each(&:disconnect!)
end
ActiveRecord::Base.clear_all_connections!
else
ActiveRecord::Base.connection_handlers.each_value do |handler|
handler.connection_pool_list.each do |pool|
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
end
end
ActiveRecord::Base.clear_active_connections!
end
end

def clear_ar_6_0_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