title: Hibernate throws NullPointerException in EntityVerifyVersionProcess
body: <p>In a <code>WebApplication</code> with Hibernate, I'm using a standard interceptor to create and commit my transactions. However, I'm getting a <code>NullPointerException</code> when the transaction is committed.</p>
  
  <p>The error hints to bad session-management, but I don't know why.<br>
  I can only find very little information about this particular error. Can anybody help?</p>
  
  <hr>
  
  <p><strong>Stacktrace:</strong></p>
  
  <pre><code>2011-04-20 10:20:57,016 ["http-bio-8080"-exec-7] ERROR org.hibernate.AssertionFailure - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
  java.lang.NullPointerException
      at org.hibernate.action.EntityVerifyVersionProcess.doBeforeTransactionCompletion(EntityVerifyVersionProcess.java:56)
      at org.hibernate.engine.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:543)
      at org.hibernate.engine.ActionQueue.beforeTransactionCompletion(ActionQueue.java:216)
      at org.hibernate.impl.SessionImpl.beforeTransactionCompletion(SessionImpl.java:571)
      at org.hibernate.jdbc.JDBCContext.beforeTransactionCompletion(JDBCContext.java:250)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:138)
      at com.innovigent.ptaserver.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:38)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:550)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  org.hibernate.AssertionFailure: Unable to perform beforeTransactionCompletion callback
      at org.hibernate.engine.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:549)
      at org.hibernate.engine.ActionQueue.beforeTransactionCompletion(ActionQueue.java:216)
      at org.hibernate.impl.SessionImpl.beforeTransactionCompletion(SessionImpl.java:571)
      at org.hibernate.jdbc.JDBCContext.beforeTransactionCompletion(JDBCContext.java:250)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:138)
      at com.innovigent.ptaserver.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:38)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:550)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.lang.NullPointerException
      at org.hibernate.action.EntityVerifyVersionProcess.doBeforeTransactionCompletion(EntityVerifyVersionProcess.java:56)
      at org.hibernate.engine.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:543)
      ... 21 more
  </code></pre>
  
  <p><strong>Interceptor class:</strong></p>
  
  <pre><code>public class HibernateSessionRequestFilter implements Filter {
  
      private static Logger log = LoggerFactory.getLogger(HibernateSessionRequestFilter.class);
  
      private SessionFactory sf;
  
      @Override
      public void destroy() {
      }
  
      @Override
      public void doFilter(ServletRequest request, ServletResponse response,
          FilterChain chain) throws IOException, ServletException {
  
      try{
      log.debug("Starting a database transaction");
      sf.getCurrentSession().beginTransaction();
      chain.doFilter(request, response);
  
      log.debug("Comitting the database transaction");
      sf.getCurrentSession().getTransaction().commit();
      }catch(Throwable ex){
          ex.printStackTrace();
  
              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);
              }
  
              // Let others handle it... maybe another interceptor for exceptions?
              throw new ServletException(ex);
      }
  
      }
  
      @Override
      public void init(FilterConfig arg0) throws ServletException {
      log.debug("Initializing HibernateSessionRequestFilter");
      sf = HibernateUtil.getSessionFactory();
      }
  
  }
  </code></pre>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
    &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
    &lt;property name="hibernate.connection.password"&gt;password&lt;/property&gt;
    &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost/db&lt;/property&gt;
    &lt;property name="hibernate.connection.username"&gt;db&lt;/property&gt;
    &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
     &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
  
  ... some reference to mapping xml files ...
  
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>