问题
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?
回答1:
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.
回答2:
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).
回答3:
In your database-configuration (eg. config/database.yml
) you can set a connection-timeout for every database-connection in seconds:
For example, to set the connection-timeout to 30 seconds:
production:
host: HOST
port: 5432
connect_timeout: 30
...
The default connection timeout seems to be 60 seconds, even though, this value may depend on the adapter you use (eg. with PostgreSQL adapter).
来源:https://stackoverflow.com/questions/16028121/how-to-increase-activerecord-connection-timeout