java事务调用事务_java – 了解Spring事务 – 事务方法调用另一个事务方法时会发生什么?...

当一个@Transactional方法调用另一个@Transactional方法时,根据Spring的事务传播行为,默认情况下,两个方法将在同一个事务中执行。如果配置保持默认,即PROPAGATION_REQUIRED,新的方法调用不会开启新的事务,而是加入到当前事务中。这意味着,myDAO.getSomeDBObjects()的操作会成为myService.myServiceMethod()事务的一部分,共同参与事务的提交或回滚。了解这些细节对于正确处理事务边界至关重要。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值