简体   繁体   中英

Tomcat Datasource configuration Connection timeout and Max Active to Idle connection ratio

I am having a web application which is load balanced on four servers. These three servers connect to common database with max connections setup to 600.

My Current database pool configuration in tomcat is as follows:

<Resource name="jdbc/AppDB"
                          auth="Container"
                          type="javax.sql.DataSource"
                          maxActive="100"
                          maxIdle="30"
                          maxWait="10000"
                          removeAbandoned ="true"
                          removeAbandonedTimeout="300"
                          testOnBorrow="true"
                          validationQuery="select 1"
                          logAbandoned ="true"
                          username="username"
                          password="password"
                          driverClassName="com.mysql.jdbc.Driver"
                          url="dbconnectionurl"
                 />

But this configuration gives Exception: Connection Timeout : Waiting for Idle Object.

I have monitored database server, my effective utilization of connections is 350 only.

If I calculate it backwards, I have total 400 connections used actively. This gives me 200 extra database connections available from DB.

Hence, I am not able to understand why this Exception is coming.

Can anybody suggest a better configuration ? What should be the ideal ratio of maxActive and maxIdle ?

UPDATE:

I found a related link: There are not enough idle connections in Tomcat JDBC pool

But I can not risk setting maxIdle or MaxActive to -1(INFINITE) because I have multiple instances running, and it might make uneven distribution of connections. This may cause one instance to perform nicely and other may fail due to lack of connections.

First off, maxIdle doesn't have anything to do with your problem, as this only defines how many connections are kept in the pool that are not actively used - excess connections will be dropped. To illustrate this: imagine that at t1 80 connections are in use; at t2 only 30 connections are still in use, meaning 50 connections are put back into the pool. The maxIdle setting of 30 now causes the pool to close 20 of the 50 idle connections.
Now if at t3 50 connections are in use again, the "idle pool" would contain only 10 connections. The number of connections in the idle pool is not actively increased!

To make that clear: maxActive is the maximum number of connections the pool provides. maxIdle does not add another bunch of connections on top of that; it is just a means to keep around a number of connections for the base load, speeding up the connection usage (if maxIdle would be 0, each connection returned to the pool would be closed, thwarting the idea of a connection pool).

To suggest a proper ratio for your case is kind of hard, as it depends on the load curve your application has to face. But for the problem you are facing (the pool does not return an idle, ie "free" connection), it wouldn't matter if you'd set it to -1 , as the pool is simply exhausted.

So to fix your problem, you either need to increase maxActive or you need to find ways to use the connections more efficiently.

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