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

后端 未结 3 1304
日久生厌
日久生厌 2020-12-30 23:48

I have a rails app in production that i deployed some changes to the other day. All of a sudden now I get the error ActiveRecord::ConnectionTimeoutError: could not obt

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 00:14

    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 code
    end
    

    Since Puma works multi-threaded, the pool size (as eabraham mentioned) can be a limitation, too. Try to increase it (a little)...

    I hope this helps!

提交回复
热议问题