File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes CommunicationsException tomcat6 and mysql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "CommunicationsException tomcat6 and mysql" Watch "CommunicationsException tomcat6 and mysql" New topic

CommunicationsException tomcat6 and mysql

Michael Oberleitner

Joined: Jul 13, 2011
Posts: 16

I have a servlet application running on tomcat 6 and Ubuntu 10.04.1 LTS and mysql. Most of the time the application runs fine, but sometimes I get the following error when users try to login:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 68.902.473 milliseconds ago. The last packet sent successfully to the server was 68.902.474 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.
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.send(
at com.mysql.jdbc.MysqlIO.sendCommand(
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
at com.mysql.jdbc.ConnectionImpl.execSQL(
at com.mysql.jdbc.ConnectionImpl.setAutoCommit(
at org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(
at com.inzeit.servlet.LoginServlet.checkCredentials(
at com.inzeit.servlet.LoginServlet.doGet(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.Http11Processor.process(
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(
Caused by: Broken pipe
at Method)
at com.mysql.jdbc.MysqlIO.send(
... 22 more

This is the code:

private void checkCredentials(HttpServletRequest request, HttpServletResponse response)
String query = "SELECT role,, name, firstName, email FROM user, employees, addresses WHERE userName = ? " +
"AND = user.employeeID AND employees.addressID = addresses.ID";

String loginName = request.getHeader("userName");
String pw = request.getHeader("passWord");
Date now = new Date();
try {
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/mysqlDB");//Here the exception gets thrown
Connection connection = dataSource.getConnection();
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setString(1, loginName);
ResultSet rslt = stmt.executeQuery();
ObjectOutputStream outstream = new ObjectOutputStream(response.getOutputStream());
Editor editor = new Editor(rslt.getInt(2), loginName, rslt.getString(1), rslt.getString(3), rslt.getString(4), rslt.getString(5));
} catch (NamingException e) {
// TODO Auto-generated catch block
} catch (SQLException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block

The exception occurs most of the time early in the morning when the application was not used for a longer time.
The users have to reconnect and after several times it works again. I checked the /var/log/error.log which was empty and the my.cnf from mysql and context.xml of tomcat, but I don't know which parameter to change.
If someone had similar problems and knows how to fix it i would appreciate.

Kind regards,
Michael Oberleitner

Joined: Jul 13, 2011
Posts: 16
I changed wait_timeout in mysql from 2800 to 28000 now and I will see if this helps for this problem.
When someone has other possible solutions or ideas i would be happy to here it.

Kind regards
Karthik Shiraly

Joined: Apr 04, 2009
Posts: 874

When mysql sees no traffic on its connections, it closes them. The timeout change will help but it's not the best solution - it's just delaying the problem.
To solve the problem, add "testOnBorrow=true" and "validationQuery=Select 1" as attributes to your JDBC pool resource. If there's been a communication shutdown due to timeout, these attributes ensure that DBCP reestablishes connections with DB automatically. Example here
Michael Oberleitner

Joined: Jul 13, 2011
Posts: 16
Thanks a lot Karthik Shiraly

I will try this soon.
I agree. Here's the link:
subject: CommunicationsException tomcat6 and mysql
It's not a secret anymore!