Monday, January 7, 2013

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

My web app was failing with this exception:  com.mysql.jdbc.exceptions.jdbc4.CommunicationsException.  Turns out it was using a database connection pool (see article at http://www.developer.com/java/data/article.php/3847901/Implement-Java-Connection-Pooling-with-JDBC.htm) and the connections were timing out.  Default timeout on MySQL is eight hours.  I fixed this by using the isValid method of the Connection class (code added to getConnectionFromPool method):

try
{
   if (connection == null || !connection.isValid(5))
   {
      connection = createNewConnectionForPool();
   }

}
catch (SQLException sqle)
{
   connection = createNewConnectionForPool();

}

return connection;

No comments:

Post a Comment