我有一个问题,我没有得到我的交易提交
我的Service类是@Transactional注解的
@Transactional
class MyService{
public void find();
public void return(); //Return will only happen if find is updated in DB
public void cancel(){
find();
return();
}
}
我的类(class)有 3 个 api,它们在内部调用各自的 Dao,而 Dao 没有被注释为事务。
现在如果我调用取消,我将首先调用 find,然后调用 return()。但是 return() 需要 find() 需要用 FIND 状态更新数据库。
但是由于在返回取消之前所有操作都发生在同一个事务上,因此事务不会被提交。有人可以帮助我了解这种情况并解决它。
我不知道在这种情况下如何使用传播。
提前致谢..
最佳答案
摘自 Spring Transcation 文档 (Spring Doc)
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.
在 cancel() 方法中对 find() 和 return() 的调用不是事务性的。如果您希望它是事务性的,您可以执行类似 applicationContext.getBean(this.getClass());
的操作并调用您的方法。
关于java - 链式事务 api 调用 spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27923326/