Tomcat-Mysql connections in connection pool after idle timeout

I am currently using tomcat 6 and MySQL 5.1.56. It uses Mysql Connector-j to get DB connections and use them. I have set up a connection pool from this link.

Everything works fine as long as I use it.

If I leave it dormant for a few hours, I cannot fulfill any requests. I am getting the following exception.

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: **No operations allowed after connection closed.**
java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: **Communications link failure**

The last packet successfully received from the server was 31,858,914 milliseconds ago.  The last packet sent successfully to the server was 11 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
    at java.lang.reflect.Constructor.newInstance(
    at com.mysql.jdbc.Util.handleNewInstance(
    at com.mysql.jdbc.SQLError.createCommunicationsException(
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.sendCommand(
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
    at com.mysql.jdbc.ConnectionImpl.execSQL(
    at com.mysql.jdbc.PreparedStatement.executeInternal(
    at com.mysql.jdbc.PreparedStatement.executeQuery(


I saw another person on the stack overflowing the same question, but didn't answer. Synchronizing database connections (connection pooling in Tomcat)

I found a solution here, but I cannot use it as it directs me to use another third party app that I don't have to do this.


Is there a way to solve this problem without resorting to deploying third party banners / apps.

I've already tried options like autoreconnect, test connection before and after connection and all other options.

This is my current setting in the conext.xml file

 <Resource name="jdbc/appdb"  auth="Container"
              maxActive="15"  maxIdle="3"
              validationQuery="SELECT 1"


What's the best solution in this case?

I couldn't find anything in the mysql logs.


source to share

3 answers

You shouldn't leave any connection open for hours.

You must have a persistence layer that gets the connection from the pool, performs the operation (s), commits or rolls back, and closes the connection to bring it back to the pool in the largest possible scope. Your application will be much more scalable if you don't limit yourself to one connection per session. Your application will be responsive enough because the pool will amortize the cost of opening each connection across all your requests.

Some pools have a configuration that allows you to test the connection before releasing it from the pool and provide a new one if it gets old. See how to do this with your pool / application server.



Add the following attributes to your resource tag

validationQuery="select 1"


and tomcat will check the connection when it is borrowed from the connection pool.



This configuration has been working for me for a while, but I am connecting to an Oracle database. I find that 30 seconds is too short to validate the Interval and timeBetweenEvictionRunsMillis, but that means the tests will run more often.

Also, I set numTestsPerEvictionRun to "-1", which means it will check all idle connections. Now the doc clearly states that this property is not being used, so it really is impossible to tell if it will help. You will have to try this to find out.

numTestsPerEvictionRun = "- 1"


All Articles