英文:
Why the @Transactional doesn't work in a web project which is based on SpringBoot?
问题
我使用SpringBoot完成了一个Web项目。无论我尝试多少次或多么努力,它仍然不会回滚。有人能帮助我吗?非常感谢!以下是代码部分:
// 控制器
@PostMapping(value = "/savePlan")
public R saveYearPlan(@RequestBody ProductPlanOutputMain productPlanOutputMain){
return planService.saveOrUpdatePlanBatch(productPlanOutputMain);
}
@Service
public class PlanServiceImpl implements PlanService {
@Override
public R saveOrUpdatePlanBatch(ProductPlanOutputMain productPlanOutputMain) {
return saveOrUpdateDailyPlan(productPlanOutputMain);
}
@Transactional(rollbackFor = Exception.class)
public R saveOrUpdateDailyPlan(ProductPlanOutputMain productPlanOutputMain){
// 将数据保存到数据库
productPlanOutputMainService.saveOrUpdatePlan(productPlanOutputMain);
// 测试回滚
throw new RuntimeException("发生了错误");
}
}
我已经尝试了很多次。
英文:
I did a web project by ussing SpringBoot.No matter how many times or how hard I try.It still doesn't rollback.Is there anyone could help me.Thank you so much!
Here are the codes.
//a Controller
@PostMapping(value = "/savePlan")
public R saveYearPlan(@RequestBody ProductPlanOutputMain productPlanOutputMain){
return planService.saveOrUpdatePlanBatch(productPlanOutputMain);
}
@Service
public class PlanServiceImpl implements PlanService {
@Override
public R saveOrUpdatePlanBatch(ProductPlanOutputMain productPlanOutputMain) {
return saveOrUpdateDailyPlan(productPlanOutputMain);
}
@Transactional(rollbackFor = Exception.class)
public R saveOrUpdateDailyPlan(ProductPlanOutputMain productPlanOutputMain){
//Save data to DB.
productPlanOutputMainService.saveOrUpdatePlan(productPlanOutputMain);
//test rollback
threw new RuntimeException("something wrong");
}
}
I've tried many times.
答案1
得分: 0
@Transactional 在从同一类调用的方法中不起作用。将其放入单独的服务类中。
英文:
@Transactional won't work for a method called from the same class. Put it in a separate service class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论