java spring 4 @Transactional嵌套事务问题

标签 java spring

我有以下类(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/

相关文章:

java - 在 Tomcat 上运行的 Java Web 应用程序中,如何将数据源绑定(bind)到 ServletContext 中的属性?

java - WSDL 端口的详细信息

java - Spring Social XML 配置

java - Spring只读事务提交数据到数据库

java - Android Studio : Adding a new java file to an existing package - "Unresolved Reference"

java - 如何获得 Java g.drawString() 高分辨率?

java - PKIX 路径构建失败 : unable to find valid certification path to requested target

java - Spring Security - Active Directory 为有效(解锁)用户返回 49 - 775(用户帐户锁定)

java - 如何从 Spring Boot 项目创建 jar,我们想在另一个 Spring Boot 应用程序中使用这个 jar?

java - Spring Redis 在使用@Cacheable 或@CachePut 时将 POJO 保存为不同的类型