【发布时间】:2020-11-16 13:49:17
【问题描述】:
在 Spring 批处理 Writer 中,我将 db 行状态从 0 更新为 1。如果发生任何异常,则更新为 2。 但是由于@transaction 回滚,我无法将状态更新为 2。
(我抛出异常触发回滚)
@Override
@Transactional
public void write(List<? extends TestEntity> enityList) throws Exception {
for(TestEntity testEntity : enityList) {
try {
testEntity.setStatus(2);
testRepository.save(testEntity);
testRepository.flush();
testMethod(testEntity); (which throws exception)
}catch (Exception exception) {
testEntity.setStatus(2);
testRepository.save(testEntity);
}
}
}
@Transactional
public void testMethod(TestEntity testEntity) throws Exception {
try{
//Some service call
//...
} catch(Exception e) {
log.error("error", e);
throw new Exception("exp");
}
}
【问题讨论】:
-
您的问题是“尽管事务被回滚,我如何存储数据?”
-
您需要单独的交易来更新状态。请给我看看Writer的代码吗
-
我尝试通过评论 write() 的@Transaction。还是不行
-
@dube 是的,这就是我想要的。
标签: spring-boot spring-batch spring-transactions