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

I had the same problems which were caused by too many open connections to the database. This can happen when you have database queries outside of a controller (in a model, mailer, pdf generator, …). I could fix it by wrapping those queries in this block which closes the connection automatically. ActiveRecord::Base.connection_pool.with_connection do # your … Read more