What is the difference between maxTotal and maxIdle in Apache Commons Pool 2?

sgokhales picture sgokhales · Feb 6, 2015 · Viewed 13.4k times · Source

I'm using the Apache Commons Pool 2 implementation to have object pool mechanism for my application.

As of now, I have set the default value of maxTotal() and maxIdle() as 10 in my code.

But I am not able to understand what is the difference between them ? What if I set the maxIdle() as a value very small (let's say 0) or very large (equal to maxTotal()) ?

Note: Apache classes internally recommends a default value of 8 to both of the above configs.

Answer

Elliott Frisch picture Elliott Frisch · Feb 6, 2015

A connection pool is a technique for sharing a limited number of database connections with an unconstrained number of simultaneous users. The maximum total connections (maxTotal) includes both active and idle connections, that is connections being used and connections not currently being used (it is the sum total of all connections). The maximum idle connections (maxIdle) are connections that are ready to be used (but are currently unused). If you set the maxTotal to 100 then a maximum 100 connections will be opened to your database at one time, if the maxIdle were set to 10 then if none of the connections are being used up to 90 connections might be released. The pool will re-connect on demand.

In a pool, the idle connections are ready and a request for a connection will not block if a connection is currently idle (the pool returns the idle connection). If no connections are idle, then the pool will block to open a connection or until a connection is returned to the pool.

In your question, with 10 for the maximum of both, ten Connection(s) will be opened and the pool will not shrink or grow.