Hi guys
Here's my problem, when i deploy my web applocation it on a server and leave it idle for a day (don't know sure the time), when a access it through a web browser i get the exception that follows bellow.
Thanks in advance.
Hibernate version: 3.2
Full stack trace of any exception that occurs:
Code:
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1244)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at com.mysql.jdbc.CommunicationsException.<init>(CommunicationsException.java:180)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2723)
at com.mysql.jdbc.MysqlIO.quit(MysqlIO.java:1401)
at com.mysql.jdbc.Connection.realClose(Connection.java:4882)
at com.mysql.jdbc.Connection.cleanup(Connection.java:2062)
at com.mysql.jdbc.Connection.finalize(Connection.java:3369)
at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)
org.hibernate.TransactionException: JDBC commit failed
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:130)
at com.commons.web.filters.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:43)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
at com.mysql.jdbc.Connection.commit(Connection.java:2285)
at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:139)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:115)
... 13 more
Name and version of the database you are using: MySQL 5
My code:
Filter:
Code:
package com.commons.web.filters;
import com.commons.util.hibernate.HibernateUtil;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HibernateSessionRequestFilter implements Filter {
private static Log fileLog = LogFactory.getFactory().getInstance(HibernateSessionRequestFilter.class);
private SessionFactory sf;
/**
* Open a new Connection for any page request.
* Any method that use hibernate, the filter gives him the same connection.
* the method proccess the request, and the end when is done, he close the connection.
*/
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
try {
fileLog.debug("Starting a database transaction");
sf.getCurrentSession().beginTransaction();
// Call the next filter (continue request processing)
chain.doFilter(request, response);
// Commit and cleanup
fileLog.debug("Committing the database transaction");
sf.getCurrentSession().getTransaction().commit();
}
catch (StaleObjectStateException staleEx) {
fileLog.error("This interceptor does not implement optimistic concurrency control!");
fileLog.error("Your application will not work until you add compensation actions!");
// Rollback, close everything, possibly compensate for any permanent changes
// during the conversation, and finally restart business conversation. Maybe
// give the user of the application a chance to merge some of his work with
// fresh data... what you do here depends on your applications design.
throw staleEx;
}
catch (Throwable ex) {
// Rollback only
ex.printStackTrace();
fileLog.error(ex);
try {
if (sf.getCurrentSession().getTransaction().isActive()) {
fileLog.debug("Trying to rollback database transaction after exception");
sf.getCurrentSession().getTransaction().rollback();
}
}
catch (Throwable rbEx) {
fileLog.error("Could not rollback transaction after exception!", rbEx);
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
fileLog.debug("Initializing filter...");
fileLog.debug("Obtaining SessionFactory from static HibernateUtil singleton");
sf = HibernateUtil.getSessionFactory();
}
public void destroy() {}
}
Helper class:
Code:
public class HibernateUtil {
/* static context */
private static final SessionFactory sessionFactory;
private static HibernateUtil hu;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static HibernateUtil getInstance() {
return new HibernateUtil();
}
...