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