Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the Tomcat Connection Pool settings

I want to know if my understanding of the Tomcat Connection pool lifecycle is correct.

For example, I have the following settings:

<Resource name="jdbc/appname" auth="Container"
type="javax.sql.DataSource" maxActive="100" 
maxIdle="30" maxWait="1000"
username="username" 
initialSize = "5"
password="password"
driverClassName="jdbc.driver.name"
url="jdbc:protocol://hostname:port/dbname"/>

When my application is deployed it has 5 connections(initial size), when all these connections are busy tomcat create and add to pool a new connection(6), this new connections limit are maxActive(100) and when 101 requests are coming, tomcat will wait 1000 ms(maxWait) and then throw TimeOutException. In some period of time only 40 connections are busy, and when one of them is free it will be destroyed because pool almost has 30(maxIdle) free connections. Am I right?

And if I am, then what is the purpose of setting maxIdle and maxActive to different values?

like image 672
Almas Abdrazak Avatar asked Oct 12 '17 06:10

Almas Abdrazak


Video Answer


1 Answers

In some period of time only 40 connections are busy, and when one of them is free it will be destroyed because pool almost has 30(maxIdle) free connections.

When 40 connections are busy and one of them becomes free, it becomes idle, resulting in the following state:

39 busy connections
1 idle connection 

The maxActive setting specifies the max amount of connections that may exist, in any state, at any given time. The maxIdle setting is more specific and only determines the max amount of idle connections.

Say that maxActive is set to 100 and at a certain point all these connections exist and are busy, then if a couple of minutes later they are all idle, you don't want to keep these 100 idle connections, because they are not doing anything besides consuming resources.

This is where the maxIdle setting comes into play. It tells the connection pool to not hold more than an X amount of idle connections. If it is set to 30, then 70 connections of the 100 idle connections are dropped.

like image 118
Luciano van der Veekens Avatar answered Oct 17 '22 10:10

Luciano van der Veekens