事务不回滚

发布于 2024-12-19 22:56:04 字数 1885 浏览 2 评论 0原文

我在我的应用程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海之角 2024-12-26 22:56:04

您所面临的情况可能是因为 autoCommit 已打开。 如果一切正常,您将必须关闭此功能

<property name="hibernate.connection.autocommit">false</property> 

并通过 session.getTransaction().commit 自己显式进行提交。

What you are facing might be because autoCommit is turned on . You will have to turn this off

<property name="hibernate.connection.autocommit">false</property> 

and explicitly do the commit yourself via session.getTransaction().commit if everything is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文