事务不回滚
我在我的应用程序中使用 OpenSessionInView。 我的目标是如果发生任何异常,用一种方法回滚数据库中的所有更改。这是我的控制器:
@RequestMapping(value="/kartazadan.do", method=RequestMethod.GET )
@Transactional(rollbackFor=Exception.class)
public ModelAndView viewGET(HttpServletRequest request,
HttpServletResponse response) throws Exception{
int id = Integer.parseInt(ServletRequestUtils.getRequiredStringParameter(request, "id"));
ModelMap modelMap = new ModelMap();
KartaZadan kartaZadan = kartaZadanDAO.getkartaZadanById(id);
kartaZadan.setZadanie("TEST10");
kartaZadanDAO.update(kartaZadan);
kartaZadan = kartaZadanDAO.getkartaZadanById(null); //here when Exception should occurr and make transaction rollback
kartaZadanDAO.update(kartaZadan);
modelMap.addAttribute("kartaZadan", kartaZadan);
setCommonFields(modelMap);
modelMap.addAttribute("errorsEnabled", false);
return new ModelAndView("kartaZadan", modelMap);
}
通过下面给出的配置,它可以工作: 在 web.xml 中:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
但是在这个策略中我无法保存/更新视图中的任何行。所以我像这样oververrided OpenSessionInViewFilter:
public class CustomOpenSessionInViewFilter extends OpenSessionInViewFilter{
@Override
public void closeSession(Session session, SessionFactory sessionFactory){
session.flush();
super.closeSession(session,sessionFactory);
}
}
现在我可以保存/更新,但事务不会回滚...如何让它们都工作?
I use OpenSessionInView in my application.
My goal is to rollback all change in database in one method if any exception occurs. Here is my controller:
@RequestMapping(value="/kartazadan.do", method=RequestMethod.GET )
@Transactional(rollbackFor=Exception.class)
public ModelAndView viewGET(HttpServletRequest request,
HttpServletResponse response) throws Exception{
int id = Integer.parseInt(ServletRequestUtils.getRequiredStringParameter(request, "id"));
ModelMap modelMap = new ModelMap();
KartaZadan kartaZadan = kartaZadanDAO.getkartaZadanById(id);
kartaZadan.setZadanie("TEST10");
kartaZadanDAO.update(kartaZadan);
kartaZadan = kartaZadanDAO.getkartaZadanById(null); //here when Exception should occurr and make transaction rollback
kartaZadanDAO.update(kartaZadan);
modelMap.addAttribute("kartaZadan", kartaZadan);
setCommonFields(modelMap);
modelMap.addAttribute("errorsEnabled", false);
return new ModelAndView("kartaZadan", modelMap);
}
With configuration given below it works:
in web.xml:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
But in this strategy I cannot save/update any row in view. So I ovverrided OpenSessionInViewFilter like this:
public class CustomOpenSessionInViewFilter extends OpenSessionInViewFilter{
@Override
public void closeSession(Session session, SessionFactory sessionFactory){
session.flush();
super.closeSession(session,sessionFactory);
}
}
Now I can save/update, but Transaction doesnt rollback... how to make them work both?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所面临的情况可能是因为 autoCommit 已打开。 如果一切正常,您将必须关闭此功能
并通过 session.getTransaction().commit 自己显式进行提交。
What you are facing might be because autoCommit is turned on . You will have to turn this off
and explicitly do the commit yourself via session.getTransaction().commit if everything is fine.