Archive for June, 2010

Solving com.mysql.jdbc.exceptions. jdbc4.CommunicationsException in Spring JDBC based application

Tags: , , , , ,

Last week after releasing first version of Wicket, Spring and JDBC based application into production I noticed strange behavior. Everyday first attempt to enter the service caused unexpected exception. Quick glance at Tomcat logs showed code presented below:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was156378 seconds ago.The last packet sent successfully to the server was 156378 seconds ago, which  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(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.
newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
        at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3246)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1917)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
        at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1383)
        at com.mysql.jdbc.ConnectionImpl.getTransactionIsolation(ConnectionImpl.java:3117)
        at org.apache.commons.dbcp.DelegatingConnection.
getTransactionIsolation(DelegatingConnection.java:313)
        at org.apache.commons.dbcp.PoolingDataSource
$PoolGuardConnectionWrapper.getTransactionIsolation(PoolingDataSource.java:239)
        at org.springframework.jdbc.datasource.DataSourceUtils.
prepareConnectionForTransaction(DataSourceUtils.java:173)
        at org.springframework.jdbc.datasource.DataSourceTransactionManager
.doBegin(DataSourceTransactionManager.java:210)
        ... 41 more

It looks that after few hours at night when no-one used the application, MySQl server closed connection and first database query caused exception. I double checked my connection url and settings in context.xml file to found nothing obviously wrong: Continue reading this post …


Be Sociable, Share!

Simple notification about errors in Wicket-based application

Tags: , ,

Today I am going to to describe a simple and quick trick in Apache Wicket which allow our web application to send email everytime when something goes wrong.

Adding some method when exception appears in Wicket-based application is very easy task. In class extending WebApplication we should create our own implementation of method creating custom RequestCycle object where we override onRuntimeException method:

@Override
public RequestCycle newRequestCycle(Request request, Response response) {
  return new WebRequestCycle(this, (WebRequest)request, (WebResponse)response) {

    @Override
    public Page onRuntimeException(Page page, RuntimeException exception) {

      //
      // do something with this exception
      //
      return super.onRuntimeException(page, exception);
    }
  };
}

Continue reading this post …


Be Sociable, Share!