Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase ActiveRecord connection timeout?

Is there a way to increase the connection timeout for ActiveRecord?

I keep getting this error when I have more than 25 threads with a pool size of 5.

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

If there is not a way to increase the connection timeout, what is the best way to ensure a thread is using a connection as quick as possible?

like image 896
Henley Avatar asked Apr 16 '13 03:04

Henley


2 Answers

According to docs you should set the "checkout_timeout" option in your database configuration file.

checkout_timeout: number of seconds to block and wait for a connection before giving up and raising a timeout error (default 5 seconds).

like image 79
barbolo Avatar answered Oct 03 '22 05:10

barbolo


You can add

pool: 5

in your database.yml.

you can also set the checkout_timeout value, but I do not recomend it, because your application might take more seconds to answer it. If the error is throw when the system is under many requests, it is probably better to just give more possible simultaneous connections instead of making each request wait longer to finish.

like image 36
fotanus Avatar answered Oct 03 '22 04:10

fotanus