我有以下类(class):
@Transactional
public class MyClass{
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void method1(){
....
myDao.update(entity);
}
public void method2(){
method1();
//I need to be sure that data was persisted to DB and find the entity by id
MyEntity ent=myDao.find(entityId);
//entity is not updated here
}
}
但实际上我无法在方法 2 中从数据库中读取更新的实体。如何做到这一点? 我需要在 method2 中在调用 method1() 后更新值,因此应该提交 method1 中的事务并且结果可见。如何做到这一点?
最佳答案
你必须在另一个类中执行此操作,因为在调用本地方法时不遵守 @Transactional
(这取决于 Spring 代理的工作方式,本地方法调用绕过事务性通过调用 this
进行代理)。
解决方案可能看起来像这样:
class Wrapper {
public void performAction() {
myClass.method1();
myClass.find(entityId);
}
}
关于java spring 4 @Transactional嵌套事务问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738347/