I am getting the following error when I try to begin a transaction on a Session.
javax.servlet.ServletException: Cannot open connection
persistence.HibernateSessionConversationFilter.doFilter(HibernateSessionConversationFilter.java:126)
root cause
org.hibernate.exception.GenericJDBCException: Cannot open connection
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:129)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1290)
persistence.HibernateSessionConversationFilter.doFilter(HibernateSessionConversationFilter.java:59)
The code is as follows:
package persistence;
import org.apache.commons.logging.*;
import org.hibernate.*;
import org.hibernate.classic.Session;
import org.hibernate.context.ManagedSessionContext;
import javax.servlet.*;
/*
* Servlet filter class must be explicity imported;
* Servlet filter class differs from the Hibernate filter class
*/
import javax.servlet.Filter;
import javax.servlet.http.*;
import java.io.IOException;
public class HibernateSessionConversationFilter implements Filter {
private static final String HIBERNATE_SESSION_KEY = "hibernateSession";
private static final String END_OF_CONVERSATION_FLAG = "endOfConversation";
private static Log log = LogFactory.getLog(HibernateSessionConversationFilter.class);
private SessionFactory sessionFactory;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Session currentSession;
// Try to get a Hibernate Session from the HttpSession
HttpSession httpSession = ((HttpServletRequest) request).getSession();
Session disconnectedSession = (Session) httpSession.getAttribute(HIBERNATE_SESSION_KEY);
try {
// Start a new conversation or in the middle?
if (disconnectedSession == null) {
log.debug(">>> New conversation");
currentSession = sessionFactory.openSession();
currentSession.setFlushMode(FlushMode.NEVER);
}
else {
log.debug("< Continuing conversation");
currentSession = (Session) disconnectedSession;
}
log.debug("Binding the current Session");
ManagedSessionContext.bind(currentSession);
log.debug("Starting a database transaction");
currentSession.beginTransaction();
log.debug("Processing the event");
chain.doFilter(request, response);
log.debug("Unbinding Session after processing");
currentSession = ManagedSessionContext.unbind(sessionFactory);
// End or continue the long-running conversation?
if ((request.getAttribute(END_OF_CONVERSATION_FLAG) != null) || (request.getParameter(END_OF_CONVERSATION_FLAG) != null)) {
log.debug("Flushing Session");
currentSession.flush();
log.debug("Committing the database transaction");
currentSession.getTransaction().commit();
log.debug("Closing the Session");
currentSession.close();
log.debug("Cleaning Session from HttpSession");
httpSession.setAttribute(HIBERNATE_SESSION_KEY, null);
log.debug("<<< End of conversation");
}
else {
log.debug("Committing database transaction");
currentSession.getTransaction().commit();
log.debug("Storing Session in the HttpSession");
httpSession.setAttribute(HIBERNATE_SESSION_KEY, currentSession);
log.debug("> Returning to user in conversation");
}
} catch (StaleObjectStateException staleException) {
log.error("This interceptor does not implement optimistic concurrency control!");
log.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 staleException;
} catch (Throwable exception) {
// Rollback only
try {
if (sessionFactory.getCurrentSession().getTransaction().isActive()) {
log.debug("Trying to rollback database transaction after exception");
sessionFactory.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable rbEx) {
log.error("Could not rollback transaction after exception!", rbEx);
} finally {
log.error("Cleanup after exception!");
// Cleanup
log.debug("Unbinding Session after exception");
currentSession = ManagedSessionContext.unbind(sessionFactory);
log.debug("Closing Session after exception");
currentSession.close();
log.debug("Removing Session from HttpSession");
httpSession.setAttribute(HIBERNATE_SESSION_KEY, null);
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(exception);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
log.debug("Initializing filter...");
log.debug("Obtaining SessionFactory from static HibernateUtil singleton");
sessionFactory = HibernateUtil.getSessionFactory();
}
public void destroy() {}
I borrowed this code from Wiki. Any help would be appreciated.
|