支持传播的 Spring 事务 [英] Spring Transactions With Supports Propagation
问题描述
我想了解使用传播支持进行弹簧交易的用途.java 文档提到,如果具有 @Transactional(propagation = Propagation.SUPPORTS)
的方法从支持该事务的事务中调用,但如果不存在事务,则该方法以非事务方式执行.
这不是 spring 事务的行为,与 Propagation.SUPPORTS
无关吗?
在上面的代码示例中,无论 methodWithSupportsTx()
是否具有 @Transactional(propagation = Propagation.SUPPORTS)
注释,它将在事务中执行取决于是否methodWithRequiredTx()
有 @Transactional
注释吧?
那么需要/使用传播级别的支持是什么?
来自 javadoc:
<块引用>注意:对于具有事务同步的事务管理器,PROPAGATION_SUPPORTS 与根本没有事务略有不同,因为它定义了同步将应用的事务范围.因此,相同的资源(JDBC 连接、Hibernate 会话等)将在整个指定范围内共享.请注意,这取决于事务管理器的实际同步配置.
因此,这意味着,例如,在 methodWithSupportsTx()
内多次调用 Hibernate 的 SessionFactory.getCurrentSession()
将返回相同的会话.
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?
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.
这篇关于支持传播的 Spring 事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!