只要了解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”.