[ACCEPTED]-ActiveRecord::ConnectionTimeoutError happening sporadically-connection-timeout

Accepted answer
Score: 14

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

Run 1 this in your console

   ActiveRecord::Base.clear_active_connections!
Score: 2

I used this code on my Sinatra app

after do
  ActiveRecord::Base.clear_active_connections!
end   

This solve 1 my problem

Score: 2

Applies also to Rails 5, since Puma is default 10 server.

If you are using Threaded Servers 9 like Puma, Phushion Passenger, they create 8 multiple threads of the same application. Thereby 7 making your application run faster, by concurrently 6 executing each incoming requests.

Make sure 5 that the pool size is equal or more than 4 the number of threads. I was having an issue 3 when few of my threads were giving me ActiveRecord::ConnectionTimeoutError, and 2 the problem was vague since it occurs once 1 in a while not very often.

Score: 2

I was also experiencing a similar problem 7 with a Sinatra App, I added

after do
  ActiveRecord::Base.clear_active_connections!
end 

To my application 6 controller and it solved my problem.

This 5 construct is known as a filter and it evaluates 4 after each request.

I'm not sure what was 3 actually happening with the application, but 2 I would suspect that connections weren't 1 being closed after each request.

More Related questions