gpt4 book ai didi

java - Spring 事务 - 需要新的 - 父级回滚

转载 作者:行者123 更新时间:2023-12-01 09:35:19 24 4
gpt4 key购买 nike

我有一项可以完成一些工作的服务:

@Service
@Transactional
public class FooServiceImpl implements FooService {

@Override
public void invoke() {
process();
}

public void process() {

// possible that will throw exception
elements.forEach -> processOne(..)

}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void processOne(..) {

// possible that will throw exception

}

}

invoke() 方法是从另一个组件调用的。据我了解,此方法在事务中运行(如果存在 - 继续,如果不存在 - 创建新的)。但我所期望的 - 方法 processOne(..) 正在新事务中运行 - 因此,如果在这个事务中一切正常,则应该提交事务。但如果发生错误,则整个过程将回滚。不仅来自当前交易。怎么了?

最佳答案

当从同一个类调用 processOne 方法时,仅当直接从其他类调用时,它才不会创建新事务。

另请参阅this question以及我的回答。

来自spring reference manual :

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional. Also, the proxy must be fully initialized to provide the expected behaviour so you should not rely on this feature in your initialization code, i.e. @PostConstruct.

关于java - Spring 事务 - 需要新的 - 父级回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39026417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com