And*_*sne 9 spring transactions propagation
我想了解使用传播支持的Spring事务的用法.java文档提到如果@Transactional(propagation = Propagation.SUPPORTS)
从事务中调用的方法支持事务,但如果不存在事务,则该方法是非事务性的.
难道这不是春季交易的行为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
注释,对吧?
那么传播级别SUPPORTS的需求/用途是什么?
Kev*_*sox -2
如果不存在所需的事务,则将创建一个新事务。因此,当您调用 serviceBean.methodWithSupportsTx() 时,将进行一个新事务。如果你的方法确实是事务性的,如果不存在事务,你将会看到来自 spring 的错误。