my problem is with Session in View code. I had just added for long time hibernate sessions, but when application entries in the same SQL for three times in about ten seconds it give me an error at last SQL access, but in the other same SQL thar executes before, the error doesn´t exists.
What could be problem?. The error code is this:
[ INFO] {02/02/06 10:11:48} (bbdd.HttpListener.sessionCreated()) - Nueva Sesión: 918791DA23E88870E976EE8FF133CADD IP: 127.0.0.1
[ INFO] {02/02/06 10:11:48} (bbdd.Hibernate.ExtendedThreadLocalSessionContext.buildOrObtainSession()) - Opening a new Session
[ INFO] {02/02/06 10:11:48} (bbdd.Hibernate.ExtendedThreadLocalSessionContext.buildOrObtainSession()) - Disabling automatic flushing of the Session
[ERROR] {02/02/06 10:11:53} (bbdd.Hibernate.HibernateThreadFilter.doFilter()) - Cleanup after exception!
[ERROR] {02/02/06 10:11:53} (catalina.core.StandardWrapperValve.invoke()) - Servlet.service() para servlet default lanzó excepción
java.lang.NullPointerException
at bbdd.Hibernate.HibernateThreadFilter.doFilter(HibernateThreadFilter.java:137)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
[ INFO] {02/02/06 10:11:53} (bbdd.Hibernate.ExtendedThreadLocalSessionContext.buildOrObtainSession()) - Opening a new Session
[ INFO] {02/02/06 10:11:53} (bbdd.Hibernate.ExtendedThreadLocalSessionContext.buildOrObtainSession()) - Disabling automatic flushing of the Session
It use bind and unbind ok in the first two or three DB access, but after that it close sessionFactory and create another new.
/*
* HibernateThreadFilter.java
*
* Created on 11 de enero de 2006, 10:47
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package bbdd.Hibernate;
import org.apache.commons.logging.*;
import org.hibernate.*;
import javax.servlet.*;
import javax.servlet.Filter;
import javax.servlet.http.*;
import java.io.IOException;
import bbdd.Hibernate.ExtendedThreadLocalSessionContext;
/**
* A servlet filter that provides a thread-bound session-per-request.
* <p>
* This filter should be used if your <tt>hibernate.current_session_context_class</tt>
* configuration is set to <tt>thread</tt> and you are not using JTA/CMT. You can use
* this filter for a thread-bound <tt>Session</tt>, either with resource-local transactions
* (direct JDBC) or user-managed JTA transactions. Set your
* <tt>hibernate.transaction.factory_class</tt> accordingly.
* <p>
* An alternative, more flexible solution is <tt>TransactionInterceptor</tt>
* that can be applied to any pointcut with JBoss AOP.
* <p>
* Note that you should not use this interceptor out-of-the-box with enabled optimistic
* concurrency control. Apply your own compensation logic for failed conversations, this
* is totally dependent on your applications design.
*
* @see org.hibernate.ce.auction.persistence.TransactionInterceptor
*
* @author
[email protected] */
public class HibernateThreadFilter implements Filter {
private static Log log = LogFactory.getLog(HibernateThreadFilter.class);
private SessionFactory sf;
public static final String HIBERNATE_SESSION_KEY = "hibernate_session";
public static final String END_OF_CONVERSATION_FLAG = "end_of_conversation";
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Try to get a Hibernate Session from the HttpSession
HttpSession httpSession = ((HttpServletRequest) request).getSession();
Session hibernateSession = (Session) httpSession.getAttribute(HIBERNATE_SESSION_KEY);
try {
if (hibernateSession != null) {
log.debug("< Continuing conversation");
ExtendedThreadLocalSessionContext.bind(hibernateSession);
} else {
log.debug(">>> New conversation");
}
log.debug("Starting a database transaction");
sf.getCurrentSession().beginTransaction();
// Do the work...
chain.doFilter(request, response);
// End or continue the long-running conversation?
if (request.getAttribute(END_OF_CONVERSATION_FLAG) != null) {
log.debug("Flushing Session");
sf.getCurrentSession().flush();
log.debug("Committing the database transaction");
sf.getCurrentSession().getTransaction().commit();
log.debug("Closing and unbinding Session from thread");
sf.getCurrentSession().close(); // Unbind is automatic here
log.debug("Removing Session from HttpSession");
httpSession.setAttribute(HIBERNATE_SESSION_KEY, null);
log.debug("<<< End of conversation");
} else {
log.debug("Committing database transaction");
sf.getCurrentSession().getTransaction().commit();
log.debug("Unbinding Session from thread");
hibernateSession = ExtendedThreadLocalSessionContext.unbind(sf);
log.debug("Agregando sesion hibernate a la HttpSession");
httpSession.setAttribute("hibernate_session", hibernateSession);
log.debug("> Returning to user in conversation");
}
} catch (StaleObjectStateException staleEx) {
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 staleEx;
} catch (Throwable ex) {
// Rollback only
try {
if (sf.getCurrentSession().getTransaction().isActive()) {
log.debug("Trying to rollback database transaction after exception");
sf.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable rbEx) {
log.error("Could not rollback transaction after exception!" + rbEx.getLocalizedMessage(), rbEx);
} finally {
log.error("Cleanup after exception!");
// Cleanup
log.debug("Closing and unbinding Session from thread");
sf.getCurrentSession().close(); // Unbind is automatic here
log.debug("Removing Session from HttpSession");
httpSession.setAttribute(HIBERNATE_SESSION_KEY, null);
}
if (ex.getCause().getLocalizedMessage()!= null) {
if (ex.getCause().getLocalizedMessage().indexOf("pool exhausted")!= -1) {
log.warn("El pool de conexiones de la aplicación está lleno.");
}
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
log.info("Initializing filter, obtaining Hibernate SessionFactory from HibernateUtil");
sf = oConexion.getSessionFactory();
}
public void destroy() {}
}
Thanks all.