支持传播的 Spring 事务

发布于 2024-11-17 03:08:15 字数 867 浏览 1 评论 0原文

我想了解 Spring 事务与传播支持的用途。 java 文档提到,如果从事务中调用具有 @Transactional(propagation = Propagation.SUPPORTS) 的方法,则它支持该事务,但如果不存在事务,则该方法将以非事务方式执行。

这不是 Spring 事务的行为,与 Propagation.SUPPORTS 无关吗?



public class ServiceBean {

    @Transactional(propagation = Propagation.SUPPORTS)
    public void methodWithSupportsTx() {
        //perform some database operations
    }
}

public class OtherServiceBean {

    @Transactional(propagation = Propagation.REQUIRED)
    public void methodWithRequiredTx() {
        //perform some database operations
        serviceBean.methodWithSupportsTx();
    }
}


在上面的代码示例中,无论 methodWithSupportsTx() 是否具有 @Transactional(propagation = Propagation.SUPPORTS) 注解,它都会在事务中执行,具体取决于 methodWithRequiredTx()@Transactional 注释,对吧?

那么传播级别支持的需要/用途是什么?

I would like to understand the use of having a spring transaction with Propagation Supports. The java docs mention that if the method which has @Transactional(propagation = Propagation.SUPPORTS) is called from within a transaction it supports the transaction but if no transaction exists, the method is executed non-transactionally.

Isn't this already the behavior of spring transactions irrespective of Propagation.SUPPORTS?



public class ServiceBean {

    @Transactional(propagation = Propagation.SUPPORTS)
    public void methodWithSupportsTx() {
        //perform some database operations
    }
}

public class OtherServiceBean {

    @Transactional(propagation = Propagation.REQUIRED)
    public void methodWithRequiredTx() {
        //perform some database operations
        serviceBean.methodWithSupportsTx();
    }
}


In the above code example, irrespective of whether methodWithSupportsTx() has @Transactional(propagation = Propagation.SUPPORTS) annotation it would be executed in a transaction depending on whether methodWithRequiredTx() has @Transactional annotation, right?

So what's the need/use of having a propagation level SUPPORTS?

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

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

发布评论

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

评论(2

橙幽之幻 2024-11-24 03:08:15

来自 javadoc

注意:对于具有事务同步的事务管理器,PROPAGATION_SUPPORTS 与根本没有事务略有不同,因为它定义了同步将应用的事务范围。因此,相同的资源(JDBC 连接、Hibernate 会话等)将在整个指定范围内共享。请注意,这取决于事务管理器的实际同步配置。

因此,这意味着,例如,在 methodWithSupportsTx() 内多次调用 Hibernate 的 SessionFactory.getCurrentSession() 将返回相同的会话。

From javadoc:

Note: For transaction managers with transaction synchronization, PROPAGATION_SUPPORTS is slightly different from no transaction at all, as it defines a transaction scope that synchronization will apply for. As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) will be shared for the entire specified scope. Note that this depends on the actual synchronization configuration of the transaction manager.

So, it means that, for example, multiple invocations of Hibernate's SessionFactory.getCurrentSession() inside methodWithSupportsTx() would return the same session.

暗藏城府 2024-11-24 03:08:15

如果不存在所需的事务,则将创建一个新事务。因此,当您调用 serviceBean.methodWithSupportsTx() 时,将进行一个新事务。如果你的方法确实是事务性的,如果不存在事务,你将会看到来自 spring 的错误。

A required transaction will create a new transaction if none exists. Therefore a new transaction would be made when you call serviceBean.methodWithSupportsTx(). If your method is truly transactional you will see an error from spring if no transaction exists.

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