I have a Java application under Tomcat, connecting to a Postgre and a MySQL databases.
Every first time that I access the database from Java (after a while), it always fails. I have check Tomcat’s log and found this:
The last packet sent successfully to the server was 85,313,128 milliseconds ago. is
longer than the server configured value of ‘wait_timeout’. You should consider either
expiring and/or testing connection validity before use in your application, increasing
the server configured values for client timeouts, or using the Connector/J connection
property ‘autoReconnect=true’ to avoid this problem.
I have checked this question
but don’t really understand the solution (or if it applies to my case) or why this is happening.
I suppose the problem is that the Postgre/MySQL database was idle for too long, but when I access the database with Java I open a connection and always close it when finished, so I do not understand why the connection should remain open for so long.
Does Anyone have any ideas?
UPDATE:
I am not using C3PO… I connect with:
Datasource d = context.lookup("....");
Connection c = d.getConnection();
And disconnect with:
c.close();
And the context for MySQL looks like:
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="200" maxIdle="15" maxWait="-1" name="jdbc/project" password="...." type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/project" username="..."/>
<ResourceLink global="jdbc/project" name="jdbc/project" type="javax.sql.DataSource"/>
I’m guessing you’re using a connection pool to get connections to MySQL. The principle of a connection pool is that it opens a bunch of connections, and gives you one of them when you ask for. When you close the connection, the physical connection is not closed, but the connection returns to the pool in order to be used by another request. This allows much better performance because you avoid opening and closing connections endlessly: opening a connection is costly.
The problem is that is one of the connection stays in the pool without being used for too much time, MySQL considers that it’s not useful to let it open anymore, and closes it.
To avoid this, you can