2011-04-20 96 views
2

在使用Hibernate的WebApplication中,我使用標準攔截器來創建和提交我的事務。但是,當交易被提交時,我得到一個NullPointerExceptionHibernate在EntityVerifyVersionProcess中拋出NullPointerException

錯誤的會話管理提示,但我不知道爲什麼。
我只能找到有關此特定錯誤的很少信息。任何人都可以幫忙嗎?


堆棧跟蹤:

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 

攔截類:

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(); 
    } 

} 

的hibernate.cfg.xml

<hibernate-configuration> 
<session-factory> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.password">password</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost/db</property> 
    <property name="hibernate.connection.username">db</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.current_session_context_class">thread</property> 

... some reference to mapping xml files ... 

</session-factory> 
</hibernate-configuration> 

回答

2

此錯誤是由於我已將標準查詢的LockMode設置爲LockMode.OPTIMISTIC而引起的。刪除此設置解決了這個問題..

0

你有沒有

爲了使線程綁定策略 Hibernate配置:設置 hibernate.transaction.factory_class來 org.hibernate.transaction.JDBCTransactionFactory 設置 的hibernate.current_session_context_class 線程

(從the docs

另外,當我理解文檔權限時,會話被放入線程過濾器中,因此所有調用的方法都處於打開狀態。你確定你沒有弄清楚Hibernate會話嗎(比如稍後獲取會話並提交它)?

+0

我的hibernate.cfg.xml包含正確的參數: 「<屬性名=」 的hibernate.current_session_context_class 「>螺紋」 我總是用使用getCurrentSession() - 函數在我的DAO對象,和我只在該會話上運行一些查詢,沒有別的。 – wlcrs 2011-04-20 19:39:13

0

您是否已將@Version和long類型的字段添加到實體類以支持樂觀鎖定?

@Version 
public long version; 
相關問題