帮忙看看这段代码。谢谢大家
public class HibernateFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
private SessionFactory sf;
private Logger logger = LogManager.getLogger(HibernateFilter.class);
public void init(FilterConfig filterConfig) throws ServletException {
logger.debug("初始化session factory");
sf = HibernateUtil.getSessionFactory();
}
//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException,
IOException {
logger.debug("进入hibernate filter");
try {
logger.debug("开启事务");
sf.getCurrentSession().beginTransaction();
chain.doFilter(request, response);
logger.debug("关闭事务");
sf.getCurrentSession().getTransaction().commit();
} catch (Throwable ex) {
ex.printStackTrace();
try {
if (sf.getCurrentSession().getTransaction().isActive()) {
logger.debug("发生异常,回滚事务");
sf.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable rbEx) {
logger.debug("发生异常,回滚事务失败");
rbEx.printStackTrace();
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
} finally {
logger.debug("结束hibernate filter");
}
}
//Clean up resources
public void destroy() {
}
}
这段代码中在过滤器中获取session1打开关闭事务,那么在过滤后提交到目标资源中后,在打开session2做一些数据库操作,返回过滤器用session1提交事务,那么session2所做的数据库操作能提交吗?
我觉得好像不能。请大家指教