ActiveRecord::ConnectionTimeoutError happening sporadically
C

4

24

Whenever I have an application using ActiveRecord I get this ConnectionTimeoutError - but always after a certain unknown period of time

ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5 seconds.  The max pool size is currently 30; consider increasing it.):

It was previously set to 5, we have already increased it, and there is no way it can be using 30 connections at the same time. The only thing we use ActiveRecord for is our session store.

Our database.yml file looks like:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 30
  timeout: 5000

(Test and production settings are the same)


I have been googling this occurrence, and just came across this posting:

https://groups.google.com/forum/#!msg/copenhagen-ruby-user-group/GEHgi_WudmM/gnCiwWqmVfMJ

Which mentions that ActiveRecord does not check a connection back into the pool once it is done with it?? Is that true? Do I need to manually manage the connections?

I appreciate any advice!!

edit I should probably mention I am running Rails 3.1.3

Coulometer answered 20/8, 2012 at 21:31 Comment(4)
Your sqlite gem is good and up to date? Do sqlite file have good permissions?Cubism
run "rake middleware" in your terminal and look for ActiveRecord::ConnectionAdapters::ConnectionManagementSorcerer
@KyleC - please see my comment on your post below. @Cubism - The gems in our Gemfile (related to sqlite and databases) are activerecord-jdbc-adapter using version 1.2.2, activerecord-jdbcsqlite3-adapter using version 1.2.2, and jdbc-sqlite3 using version 3.7.2Coulometer
To be honest, I have come into this project after the Gemfile was created with these Gems - is it possible that we don't need all 3 of those?Coulometer
S
15

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!
Sorcerer answered 22/8, 2012 at 22:31 Comment(10)
I assumed you meant run "rake middleware"? as your comment above states? Here is the output of the rake middleware command: note ActiveRecord::ConnectionAdapters::ConnectionManagement is listed. ... use ActionDispatch::Reloader use ActionDispatch::Callbacks **use ActiveRecord::ConnectionAdapters::ConnectionManagement** use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActiveRecord::SessionStore Coulometer
Sorry the output is a little messy in a comment, and it was too long so I removed some of the stuff from the beginning and the end of the output - but my point is ActiveRecord::ConnectionAdapters::ConnectionManagement is listed :)Coulometer
in your console, try running ActiveRecord::Base.clear_active_connections!Sorcerer
yup! What am I looking for as a result of doing this? Is this something I would need to add into something like an after_filter? That was something that I found as a suggestion online, but then I am essentially managing the connections myself (ish anyways, at least I am clearing them manually)Coulometer
No the rails middleware should automatically be clearing the active connection with each request, try clear all the active ones in the console and see if the error occurs again. You should not have to manage the connections manually.Sorcerer
I have done that - and I have not yet seen the error - yet sometimes I forget how long my rails dev environment has been running for (we have never actually seen this problem in production yet) - Thanks for your help!!Coulometer
Good to hear, I updated my answer. Can you mark it as the solution? ThanksSorcerer
I also has to restart my apache/passenger before everything worked again.Bukharin
This is only the answer if you want to clear connections right now - it won't stop it happening again.Scruple
Just to add to the above: I had an issue where a gem (rollbar) installed as part of a Rails 4 build, resulted in connections not being closed correctly. Correct fix in this instance would be to patch, or remove the gem.Scruple
V
2

I used this code on my Sinatra app

after do
  ActiveRecord::Base.clear_active_connections!
end   

This solve my problem

Vicennial answered 13/5, 2014 at 14:32 Comment(1)
Where do you put this? In your application controller?Barmy
K
2

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

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

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

Kershaw answered 6/2, 2016 at 12:33 Comment(1)
This worked for me. I've been running my rails app with default pool count (which is 5), while using Puma.Causalgia
C
2

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

after do
  ActiveRecord::Base.clear_active_connections!
end 

To my application controller and it solved my problem.

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

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

Cortneycorty answered 17/1, 2017 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.