From 3eda2718f8c7d06f26dfa88b55ef637f46b5834c Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 26 Mar 2014 00:25:36 +0400 Subject: [PATCH] #345: fixed: could not obtain a database connection within 5.000 seconds --- config/application.rb | 5 +++++ lib/close_ar_connections_middleware.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 lib/close_ar_connections_middleware.rb diff --git a/config/application.rb b/config/application.rb index 225efb3dd..08dfc07f9 100644 --- a/config/application.rb +++ b/config/application.rb @@ -14,6 +14,11 @@ module Rosa class Application < Rails::Application config.i18n.enforce_available_locales = true + unless Rails.env.test? + require 'close_ar_connections_middleware' + config.middleware.insert_after('Rack::Sendfile', CloseArConnectionsMiddleware) + end + # Rate limit config.middleware.insert_before Rack::Runtime, ApiDefender diff --git a/lib/close_ar_connections_middleware.rb b/lib/close_ar_connections_middleware.rb new file mode 100644 index 000000000..e491a8a23 --- /dev/null +++ b/lib/close_ar_connections_middleware.rb @@ -0,0 +1,12 @@ +# "Coffee's for closers" +class CloseArConnectionsMiddleware + def initialize(app) + @app = app + end + + def call(env) + @app.call(env) + ensure + ActiveRecord::Base.clear_active_connections! unless env.key?('rack.test') + end +end