From b66b5378baf3804e9985a7cb9630f6d5a3ddcf97 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Fri, 17 Feb 2017 23:56:48 +0000 Subject: Clear active connections after initialization Any connections that were checked out during initialization should be checked back in before the first request is processed, for two reasons: - Returning the connection to the pool allows it to be health checked before it's used again. If the connection dies before the first request arrives, the health check will replace it with a new one. - If the thread that initialized Rails is not the same thread that will be performing work, checking in the connection will allow it to be reused instead of being stuck to the initialization thread forever. --- activerecord/lib/active_record/railtie.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/lib/active_record/railtie.rb') diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 0276d41494..73518ca144 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -166,5 +166,13 @@ end_warning path = app.paths["db"].first config.watchable_files.concat ["#{path}/schema.rb", "#{path}/structure.sql"] end + + initializer "active_record.clear_active_connections" do + config.after_initialize do + ActiveSupport.on_load(:active_record) do + clear_active_connections! + end + end + end end end -- cgit v1.2.3