Release ActiveRecord connection before Sidekiq shuts down

This question is about performance and optimization issues with how Sidekiq jobs can be implemented.

Suppose we have the following workflow

def execute

# Step 1
do_things_with_activerecord_db_and_get_some_parameters()

# Step 2
step2_perform_an_http_request_with_these_parameters_on_unreliable_server()

end

In the following case, the ActiveRecord connection is taken from the pool in step 1 and only SideKiq is freed after the task completed (or failed) with ActiveRecord SideKiq middleware.

Since the external HTTP server in step 2 where we execute this request is unreliable, the HTTP request can take a long time or even timeout, and thus the ActiveRecord connection is blocked by nothing during all this time, right?

So my question is: is it advisable, useful and safe to call:

ActiveRecord:: Base.clear_active_connections!

1 2, ? - ? redis?

!

+2
1

clear_active_connections!.

ActiveRecord , JMS TorqueBox, - , .

, Thread, ActiveRecord, ( ActiveRecord 3.2, ) .

, , :

def with_connection(&block)
  ActiveRecord::Base.connection_pool.with_connection do
    yield block
  end
ensure
  ActiveRecord::Base.clear_active_connections!
  ActiveRecord::Base.connection.close
end

:

with_connection do 
  do_things_with_activerecord_db_and_get_some_parameters()   
end
+2

All Articles