简体   繁体   中英

There are not enough idle connections in Tomcat JDBC pool

Given the following Tomcat JDBC connection settings:

<Resource name="jdbc/pc4"
            maxActive="200"
            maxIdle="100"
            minIdle="50"
            initialSize="50"
            maxWait="15000"
            auth="Container"
            type="javax.sql.DataSource"
            username="....."
            password="....."
            testOnBorrow="true"
            testWhileIdle="true"
            validationQuery="select 1"
            driverClassName="com.mysql.jdbc.Driver"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            url="jdbc:mysql://server_address/db_name?autoReconnect=true&amp;autoReconnectForPools=true&amp;zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;socketTimeout=300000" />

And following MySQL parameters:

max_connections = 100000
wait_timeout = 31536000
interactive_timeout = 31536000

I would expect there to be at least 50 idle connections in connection pool at all time.

But what really happens is: there are 50 connections when the server started, after a while, all the connections die except the last one.

Is there a mistake in my configuration?

Environment:

  • Linux 3.4 64-bit
  • OpenJDK 7
  • Tomcat 7
  • MySQL 5.5

I am not an MySQL expert, but using Tomcat JDBC with Oracle for a while. Idle connections persist here as long as the app isn't shut down. I would check connecion logs in MySQL and enable logging in Tomcat JDBC to see who is really shutting down connections. Either one does.

I don't know if this is the cause of your problem, but when I use the MySQL JDBC library, I always have a problem that if a connection is opened for a long time (more than 1 hour), it randomly causes a broken pipe when I use it again.

Maybe the pool does not re-establish a connection after such an error (bug with the testWhileIdle?) Can you try to set removeAbandoned and removeAbandonedTimeout , with a value of, let's say, 30 minutes? I know it's a long shot.

This is an issue with mysql. As @Djebel said try to clean the unused connection. One question, does it start working if you restart mysql service whenever you get this error? Also make sure the all the connections get closed while shutdown the server. You can use a shutdownHook, if Tomcat doesn't do it for you.

I changed the configuration and the situation seems to be improved, there are now always more than 25 connections in connection pool (still not sure why though).

 <Resource name="jdbc/pc4"
            maxActive="-1"
            maxIdle="-1"
            minIdle="20"
            initialSize="20"
            maxWait="-1"
            auth="Container"
            type="javax.sql.DataSource"
            username="__"
            password="__"
            driverClassName="com.mysql.jdbc.Driver"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            testOnBorrow="true"
            testWhileIdle="true"
            testOnReturn="true"
            timeBetweenEvictionRunsMillis="60000"
            minEvictableIdleTimeMillis="60000"
            removeAbandoned="false"
            validationQuery="SELECT 1;"
            url="jdbc:mysql://__/__?autoReconnect=true&amp;autoReconnectForPools=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull&amp;socketTimeout=72000000" />

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