We have a problem with the mysql connection in our Server. The WebApp works correct, but after a few hours, we get an error from the connection with mysql. We think that something is wrong with the hibernate Session. As far as we know, the mysql connection is closed while the hibernate tries to use the same session. Maybe we aren't closing the session properly.
We work with Struts 2, and we have defined an Interceptor that manage all the session stuff. Here is the code:
public class OSVStrutsInterceptors implements Interceptor {
private static SessionFactory sf;
@Override
public void destroy() {
try
{
sf.getCurrentSession().close();
}
catch (HibernateException ex)
{
throw new RuntimeException(ex);
}
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation ai) throws Exception {
sf = HibernateUtil.getSessionFactory();
Session sesion=sf.getCurrentSession();
try {
sesion.beginTransaction();
}
catch (Exception ex ) {
System.out.println(new Date()+" Sesion cerrada. Obtenemos una nueva");
sesion=sf.openSession();
}
String result=ai.invoke();
sesion.getTransaction().commit();
return result;
}
}
As you can see, we are trying to get the currentSession, but if is not possible, we open a new Session. Also we close the session on the destroy() method of the interceptor.
But We always get this error on our server:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: Broken pipe
STACKTRACE:
java.net.SocketException: Broken pipe