Activerecord::Connectiontimeouterror

ActiveRecord::ConnectionTimeoutError happening sporadically

Rails has a middleware called ActiveRecord::ConnectionAdapters::ConnectionManagement which clears active connections every request so they do not stick around. Check your middleware to make sure you have this (which is there by default), run "rake middleware". You should not have to manage the connections manually to answer your last question.

Run this in your console

   ActiveRecord::Base.clear_active_connections!

ActiveRecord::ConnectionTimeoutError: could not obtain a database connection within 5.000 seconds (waited 5.000 seconds)

With the help of the devise guys' I think I finally got this issue figured out. It seemed that by using custom error pages with it's own controller, I wasn't skipping the before_action get_new_messages. So the very simple fix was to add:

skip_before_filter :get_new_messages

to my custom error controller.

This issue explains in detail the reason behind this: https://github.com/plataformatec/devise/issues/3422

ActiveRecord::ConnectionTimeoutError

As Frederick pointed out you need to return opened ActiveRecord connections to the connection pool.

If you're using the Thin server, in threaded mode, then you need to add this to your Sinatra app:

after do
ActiveRecord::Base.connection.close
end

...instead of using the ConnectionManagement suggestion. The reason is that Thin splits the request processing over 2 threads and the thread that is closing the ActiveRecord connection is not the same as the thread that opened it. As ActiveRecord tracks connections by thread ID it gets confused and won't return connections correctly.



Related Topics



Leave a reply



Submit