What is the difference between maxActive vs. maxIdle for Tomcat connection pools?
maxActive is straight forward. maxIdle can be explained in this way - say you have 100 max Active connections and say you set maxIdle to 80. Assuming there are no requests going to the database, only 80 connections will be tested (via the validationquery) and will stay active. The other 20 will be closed. So at any point you can only have 80 idle connections.
You might want to set this to be a different number to prevent additional (unnecessary) connections going out to the database. Cos every connection that is served by the database consumes resources (like memory).
But, assuming you have set the maxActive size to 100 and all 100 are in use almost all the time, this setting will obviously not matter.
Let's say you have defined the property as
maxActive = 100
which essentially means
Size of the pool = maxActive = 100
Problem with having just maxActive
Size of pool under heavy load = maxActive
Size of pool under no/low load = maxActive
Even when there is no load(none of the connections are actually in use
), all 100 connections are consuming resources
You definitely can reduce maxActive to say 80. But doing that will also mean that you are lowering the upper bound of available connections during heavy load(when application is consuming all available connections
) as well to 80.
Thus, you are stuck with maxActive as the size of the pool
How maxIdle solves the above problem
Let's say you have defined the properties as
maxActive = 100
maxIdle = 80
which essentially means
Size of pool under heavy load = maxActive = 100
Size of pool under low load = maxIdle = 80
maxIdle gives the connection pool the flexibility to adapt to load.
During high load (number of connections-in use > maxIdle
), maxActive is the only property that determines the size of the connection-pool
But during no load(number of connections in-use < than maxIdle
), maxIdle determines the size of the connection-pool
So a correct value of maxIdle ensures that connection pooling doesn't have unnecessary performance implications.
maxActive
the maximum number of active connections that can be allocated from this pool at the same time.
This attribute is used to limit the number of connections a pool can have open.
maxIdle
(int) The maximum number of connections that should be kept in the pool **at all times.**
This is to limit the idle connections. The connections(not larger than the value of maxIdle) will not be released so that the next request for connections will be much faster.
So in a word, maxActive is to limit max connections.
But idle(maxIdle
or minIdle
) is more for performance issue(exchange time with space/resources) , among which, the maxIdle is to limit the max connections(the resources) that you are going to exchange time with.