报错如下:
- Java code
严重: Servlet.service() for servlet action threw exceptionorg.hibernate.TransactionException: Transaction not successfully started at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100) at com.jadecenter.filter.HibernateFilter.doFilter(HibernateFilter.java:34) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.jadecenter.filter.ChangeCharsetFilter.doFilter(ChangeCharsetFilter.java:28) 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:230) 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:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Unknown Source)
感觉是由于我在好几个dao中的save方法中加入了:
- Java code
public void save(Project transientInstance) { log.debug("saving Project instance"); [color=#FF0000]Transaction tran = getSession().beginTransaction(); try { getSession().save(transientInstance); getSession().flush(); tran.commit(); getSession().close();[/color] log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } }
导致事物嵌套了,请大侠该如何解决这个问题,不嵌套的话,有的就保存不到数据库了!
------解决方案--------------------
方法1:将被嵌套的方法中除开事务部分代码提出来,写到一个私有方法,其它公共方法之间不要调用,公共方法只调用私有方法。
方法2:去掉所有事务代码,用spring及其它支持aop的方式管理事务