当我使用OSIVFilter时;我得到错误xD 这是过滤器代码;
Session session= HibernaterUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
try{
doFilter(servletRequest,servletResponse,filterChain);
tx.commit();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally {
HibernaterUtil.closeCurrentSession();
}
这是如何使用ThreadLocal在HibernateUtil.class中获取CurrentSession;
public static Session getCurrentSession() {
Session session = (Session) threadlocal.get();
if (session == null) {
session = sessionFactory.openSession();
threadlocal.set(session);
}
return session;
}
但最终只是得到了这个错误
org.hibernate.TransactionException: nested transactions not supported
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:154)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1471)
at com.hjy.filter.MyFilter.doFilter(MyFilter.java:26)
at com.hjy.filter.MyFilter.doFilter(MyFilter.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:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:876)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:612)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1777)
at java.lang.Thread.run(Thread.java:745)
我是hibernate的新手,任何人都可以告诉我如何解决它;
答案 0 :(得分:0)
如果&#34中有例外,请尝试抓住"阻止异常的catch块中没有rollback
你应该写下来:
Session session= HibernaterUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
try{
doFilter(servletRequest,servletResponse,filterChain);
tx.commit();
}catch(Exception e){
tx.rollback();
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally {
HibernaterUtil.closeCurrentSession();
}
您还应该检查HibernaterUtil.closeCurrentSession()
方法threadlocal
中是否已清除。