本文共 1101 字,大约阅读时间需要 3 分钟。
只要了解Spring事务的工作,我想知道在以下情况下会发生什么,一个标记为@Transactional的方法调用另一个标记为@Transactional的方法。
假设配置使用所有默认设置。
@Service("myService")
@Transactional
public MyService{
public void myServiceMethod(){
myDAO.getSomeDBObjects();
}
}
@Repository("myDAO")
@Transactional
public MyDAOWithUsesBeyondMyService{
public void getSomeDBObjects(){...}
}
现在如果我要输入MyService.myServiceMethod(),它将显然启动一个事务。那么在钻入myDAO.getSomeDBObjects()会发生什么?事实已经存在的事实不会导致新的交易出生,还是我在这里创建两个交易?
传播中的文档(引用如下)似乎涵盖了这一点,但是我想验证我的理解,对于我的处女大脑来了解一下,这是一个很大的一部分。
Propagation: Typically, all code
executed within a transaction scope
will run in that transaction. However,
you have the option of specifying the
behavior in the event that a
transactional method is executed when
a transaction context already exists.
For example, code can continue running
in the existing transaction (the
common case); or the existing
transaction can be suspended and a new
transaction created. Spring offers all
of the transaction propagation options
familiar from EJB CMT. To read about
the semantics of transaction
propagation in Spring, see Section
10.5.7, “Transaction propagation”.
转载地址:https://blog.csdn.net/weixin_33510225/article/details/114827889 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!