Stop using `ActiveRecord::Base.connection` directly
Using this method will leak connections as they are not explicitly checked out. This in turn can lead to Sidekiq threads running out of available connections (e.g. as seen in https://sentry.gitlap.com/gitlab/gitlabcom/issues/19879/ and described in https://gitlab.com/gitlab-com/infrastructure/issues/1139#note_23077361). To work around this we need to replace all uses of
ActiveRecord::Base.connection
with:
ActiveRecord::Base.connection_pool.with_connection { |connection| ... }
This ensures the connection is checked out immediately once we're done with it.