Spring嵌套@Transactional方法和回滚

发布于 2024-12-14 08:05:48 字数 926 浏览 4 评论 0原文

我有一个 @Service 类,它有一个 @Transactional 方法,该方法调用同一类上的另一个 @Transactional 方法。我正在为此测试回滚行为,发现它无法正常工作。代码如下所示:

@Service
public class DefaulService implements ervice
{
    @Transactional
    public void methodOne()
    {
        methodTwo();

            //question edited
            //this seems to be the problem
            this.serviceDAO.executeUpdateOperation();

        //test rollback
        throw new RuntimeException();
    }

    @Transactional
    public void methodTwo()
    {
        //DAO stuff
    }
}

运行 methodOne 后,我检查数据库并发现更改存在,即使日志显示“JDBCTransaction - rollback”。

如果我单独调用 methodTwo 并在其末尾添加异常,则更改将正确回滚。

有没有办法让 methodOne 正确回滚嵌套 @Transactional 调用期间发生的更改?我的印象是 REQUIRED 的默认传播可以实现这一点,但它似乎不起作用。谢谢

更新

好的,我刚刚注意到了其他事情。在异常抛出之前,我调用服务的 dao 并通过“executeUpdate”执行手动更新。如果我评论这一行,嵌套回滚就会起作用。所以看来问题实际上是调用 DAO 并运行executeUpdate 查询。但这不应该也在当前事务中运行吗?

I have a @Service class which has a @Transactional method that calls another @Transactional method on the same class. I was testing rollback behavior for this and I found that it wasn't working properly. The code looks something like this:

@Service
public class DefaulService implements ervice
{
    @Transactional
    public void methodOne()
    {
        methodTwo();

            //question edited
            //this seems to be the problem
            this.serviceDAO.executeUpdateOperation();

        //test rollback
        throw new RuntimeException();
    }

    @Transactional
    public void methodTwo()
    {
        //DAO stuff
    }
}

After running methodOne I check the database and the changes are there, even though the log shows "JDBCTransaction - rollback".

If I call methodTwo individually and add an exception at the end of it, the changes are rolled back correctly.

Is there a way to make methodOne properly rollback changes that occurred during the nested @Transactional call? I was under the impression that the default propagation of REQUIRED would achieve this, but it doesn't seem to be working. Thanks

UPDATE

Ok, I just noticed something else. Right before the exception throw, I'm calling the service's dao and performing a manual update via 'executeUpdate'. If I comment this line, the nested rollback works. So it seems that the problem is actually calling the DAO and running executeUpdate query. But shouldn't this also run inside the current transaction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

热情消退 2024-12-21 08:05:48

当你调用这些方法时,你肯定是从bean工厂获取“服务”的实例,对吧? bean 工厂需要设置一个代理来实现每个方法调用的事务逻辑。我的印象是,这只在“外部人员”通过代理调用方法时才起作用,而当一个方法调用另一个方法时不一定起作用,因为该方法是实现对象内部的直接调用,并且不通过 AOP 代理。

You are definitely obtaining the instance of the "ervice" from the bean factory when you call the methods, right? The bean factory needs to set up a proxy which implements the transactional logic around each method call. I was under the impression this only worked when "outsiders" invoke methods via the proxy, and doesn't necessarily work when one method calls another, as that method is a direct call inside the implementation object and does not go via the AOP proxy.

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