From 7e3d43686f4a6017553a636b9677b212349e35e1 Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Thu, 18 May 2023 14:00:22 +1000 Subject: [PATCH] Don't clear ActiveRecord connections when run_synchronously is enabled --- lib/que/active_record/connection.rb | 2 +- spec/que/active_record/connection_spec.rb | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/que/active_record/connection.rb b/lib/que/active_record/connection.rb index cf8dc83d..aafeb878 100644 --- a/lib/que/active_record/connection.rb +++ b/lib/que/active_record/connection.rb @@ -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 diff --git a/spec/que/active_record/connection_spec.rb b/spec/que/active_record/connection_spec.rb index 59886855..21458d92 100644 --- a/spec/que/active_record/connection_spec.rb +++ b/spec/que/active_record/connection_spec.rb @@ -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 @@ -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