Skip to content

Commit

Permalink
Don't clear ActiveRecord connections when run_synchronously is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgi committed May 18, 2023
1 parent 531b791 commit 7e3d436
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/que/active_record/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def call(job)
# feature to unknowingly leak connections to other databases. So,
# take the additional step of telling ActiveRecord to check in all
# of the current thread's connections after each job is run.
::ActiveRecord::Base.clear_active_connections!
::ActiveRecord::Base.clear_active_connections! unless job.class.resolve_que_setting(:run_synchronously)
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions spec/que/active_record/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
describe Que::ActiveRecord::Connection::JobMiddleware do
before do
Que.connection = ::ActiveRecord
end

it "should clear connections to secondary DBs between jobs" do
class SecondDatabaseModel < ActiveRecord::Base
establish_connection(QUE_URL)
end
Expand All @@ -49,10 +47,20 @@ def run(*args)
SecondDatabaseModel.connection.execute("SELECT 1")
end
end
end

it "should clear connections to secondary DBs between jobs" do
SecondDatabaseModelJob.run

refute SecondDatabaseModel.connection_handler.active_connections?
end


it "shouldn't clear connections to secondary DBs between jobs if run_synchronously is enabled " do
Que::Job.run_synchronously = true
SecondDatabaseModelJob.run

assert SecondDatabaseModel.connection_handler.active_connections?
end
end
end

0 comments on commit 7e3d436

Please sign in to comment.