Java Spring 4 @Transactional 嵌套事务问题

4

我有以下类:

@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中我无法从数据库中读取更新后的实体。如何实现这一点? 我需要在调用method1()后在method2中拥有更新后的值,因此方法1中的事务应该被提交并且结果应该可见。如何做到这一点?


由于在类级别上使用了 @Transactional,method2 和 method1 将在同一个事务中运行。在 method1 之后不会有提交,但是您应该在 method2 中看到 method1 的保存/更新结果。如果没有看到,则问题可能出在其他地方... - František Hartman
2个回答

2

由于Spring代理的工作方式,调用本地方法会绕过事务代理并调用this,因此必须从另一个类中执行此操作,因为在调用本地方法时不遵守@Transactional。

解决方案可能如下所示:

class Wrapper {
    public void performAction() {
       myClass.method1();
       myClass.find(entityId);
    }
}

1
我已经创建了一个与您场景类似的情况(嵌入式数据库): 首先,我向数据库中添加任意内容,例如:
 public void initialize() {
    Sample startEntity = new Sample();
    startEntity.setId(1);
    startEntity.setName("Start name");
    sampleRepository.saveSample(startEntity);
    sampleRepository.flush(); // <-- just to make sure scenario is recreated
    sampleRepository.clear(); // same as above
    LOGGER.info(sampleRepository.findSampleById(1));
    sampleRepository.clear(); // same as above above :D
}

在此之后,我们在数据库中得到了一个实体样本(所有事务都已完成并清除缓存);
控制台:
Hibernate: insert into sample (name, id) values (?, ?)
Hibernate: select sample0_.id as id1_0_0_, sample0_.name as name2_0_0_ from sample sample0_ where sample0_.id=?
2016-04-20 15:58:21.762  INFO 5764 --- [           main] com.patrykwoj.service.BasicServiceTest   : Sample [id=1, name=Start name]

现在,您的示例:
@Transactional
@Component
public class SampleService {

private static final Logger LOGGER = Logger.getLogger(SampleService.class);

@Autowired
SampleRepository sampleRepository;

@Transactional (propagation = Propagation.REQUIRES_NEW)
public void method1() {
    Sample someSample = new Sample();
    someSample.setId(1);
    someSample.setName("TestSample before update but after create");
    sampleRepository.updateSample(someSample);
}

public void method2() {
    method1();
    // I need to be sure that data was persisted to DB and find the entity by id

    Sample someSampleAfterUpdate = sampleRepository.findSampleById(1); //I believe that at that point sample is found in L-1 cache not in db directry.
    // entity is not updated here
    LOGGER.info(someSampleAfterUpdate); //in this point, transaction is not over yet, so you wont notice change in database..
}
}

然后从您的代码执行中控制台输出:
Hibernate: select sample0_.id as id1_0_0_, sample0_.name as name2_0_0_ from sample sample0_ where sample0_.id=?
2016-04-20 16:02:17.903  INFO 5044 --- [           main] com.patrykwoj.service.SampleService      : Sample [id=1, name=TestSample before update but after create]
Hibernate: update sample set name=? where id=?
2016-04-20 16:02:17.903  INFO 5044 --- [           main] com.patrykwoj.StackOverfloApplication    : Method2 is over

主类:

@Override
public void run(String... strings) throws Exception {
  basicServiceTest.initialize();
  sampleService.method2();
  LOGGER.info("Method2 is over");
}

在我看来一切看起来都很好。它按照预期工作。我在你的代码上做了一些评论,但控制台输出应该很清晰。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接