Ruby连数据库的问题
参考自: https://devcenter.heroku.com/articles/concurrency-and-database-connections
Sequel
和puma连用,如果报Sequel::PoolTimeout错误,可能是的puma -t的max_threads的值大于了max_connections的值(默认为4, 见 http://sequel.jeremyevans.net/rdoc/classes/Sequel/ThreadedConnectionPool.html ).
ActiveRecord
Puma
同样, puma -t的值大于了pool,也会报同样的问题: ActiveRecord::ConnectionTimeoutError - could not obtain a database connection within 5 seconds. The max pool size is currently 5; consider increasing it
pool的值应该和puma_max_threads一致. 这里reaping_frequency不能少,可以确保数据库的自动重连.
Sneakers
Sneakers里面的configuration中有threads数量, default value is 10.
So sneakers is multiple processes with multiple threads each process.
Please make sure the database connection pools of ActiveRecord has been set to the same value of threads in configuration of Sneakers(default should be 10)!
Like this:
But this is still not enough. When there is a big concurrency, you may still have that issue.
So this would work:
Refus_scheduler
Refus_scheduler is one process with multiple threads.
Each .every
.cron
etc will create a new thread.
So database connections pool
should be set to at least the threads count.
You should count the threads count by yourself.