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
Homebrew Installation on MAC Os X Failed to Connect to Raw.Githubusercontent.Com Port 443
Doing a Http Basic Authentication in Rails
How to Disable Db:Schema:Dump for Migrations
Ruby Gets/Puts Only for Strings
Disable Sprockets Asset Caching in Development
Why Is Gets Throwing an Error When Arguments Are Passed to My Ruby Script
Ruby, Remove Last N Characters from a String
How to Make Rails 3.1 Use SASS (Over SCSS) as the Default
How to Easily Parse a Url with Parameters in a Rails Test
How to Customize Rails Activerecord Validation Error Message to Show Attribute Value
Elasticsearch & Tire: Using Mapping and To_Indexed_JSON
Ruby Block to String Instead of Executing
How to Deal with Memory Leaks in Rmagick in Ruby