diff --git a/lib/after_commit_everywhere.rb b/lib/after_commit_everywhere.rb index 9987d2f..5d52f29 100644 --- a/lib/after_commit_everywhere.rb +++ b/lib/after_commit_everywhere.rb @@ -125,7 +125,7 @@ def register_callback(connection: nil, name:, without_tx:, callback:) # Helper method to determine whether we're currently in transaction or not def in_transaction?(connection = nil) # Don't establish new connection if not connected: we apparently not in transaction - return false unless connection || ActiveRecord::Base.connection_pool.connected? + return false unless connection || ActiveRecord::Base.connection_pool.active_connection? connection ||= default_connection # service transactions (tests and database_cleaner) are not joinable diff --git a/spec/after_commit_everywhere_spec.rb b/spec/after_commit_everywhere_spec.rb index a989d2d..eb6b9f1 100644 --- a/spec/after_commit_everywhere_spec.rb +++ b/spec/after_commit_everywhere_spec.rb @@ -23,7 +23,7 @@ subject do example_class.new.after_commit do handler.call - expect(ActiveRecord::Base.connection.transaction_open?).to(be_falsey) if ActiveRecord::Base.connection_pool.connected? + expect(ActiveRecord::Base.connection.transaction_open?).to(be_falsey) if ActiveRecord::Base.connection_pool.active_connection? end end @@ -186,6 +186,14 @@ expect { subject }.not_to change { ActiveRecord::Base.connection_pool.connections.size } end end + + context "when connection to the database has been established in another thread" do + before { ActiveRecord::Base.connection } + + it "doesn't leak connections" do + expect { Thread.new { subject }.join }.not_to change { ActiveRecord::Base.connection_pool.connections.size } + end + end end describe "#before_commit" do