gpt4 book ai didi

支持传播的 Spring 事务

转载 作者:行者123 更新时间:2023-12-04 04:27:13 28 4
gpt4 key购买 nike

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

不管 Propagation.SUPPORTS,这是否已经是 spring 交易的行为? ?


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注释,对吗?

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

最佳答案

来自 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.



所以,这意味着,例如,多次调用 Hibernate 的 SessionFactory.getCurrentSession()methodWithSupportsTx()将返回相同的 session 。

关于支持传播的 Spring 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6437828/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com