带有支持传播的春季交易 [英] Spring Transactions With Supports Propagation

查看:92
本文介绍了带有支持传播的春季交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解使用Propagation Supports进行春季交易的用法. Java文档提到如果从事务内调用具有@Transactional(propagation = Propagation.SUPPORTS)的方法,则该方法支持该事务,但是如果不存在任何事务,则该方法将以非事务方式执行.

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.

这是否已经成为春季交易的行为,而与Propagation.SUPPORTS无关?

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();
    }
}


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

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?

推荐答案

来自

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

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.

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

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

这篇关于带有支持传播的春季交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆