简体   繁体   中英

Rails connection pool size for WEBrick

I am getting

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

when I try to run WEBrick (rails server) with the pool size 1, and no problems with higher pool sizes.

What does WEBrick use the first connection on, and what is the best pool size for a single-threaded application? Is this a WEBrick-specific issue, or it applies to any other servers (like Unicorn)?

Rails version is 3.2.13

Update. Just verified this with unicorn, it works fine with single connection.

If I recall correctly, Rails reserves a connection to the database when it boots up and uses the remainder of the connections available in the connection pool to handle requests. Even if you never touch ActiveRecord objects during the life of the request, Rails will still try to reserve one connection from the pool for each request, or will block until one is available up until the timeout limit.

The default pool size is 5 connections: 1 reserved for Rails + 4 available for requests.

Rails does this to maintain thread-safety in the application.

If your application is single-threaded and only processes one request at a time with no regard for concurrency, the number of connections in the pool should be set to 2 at an absolute minimum. I'd still recommend the default of 5 though so you have some breathing room in case you need to utilize more than one connection per request.

This is not specific to WEBrick. The connection pool limit affects the application the same regardless of what application server is running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM