为什么在基于SpringBoot的Web项目中@Transactional不起作用?

huangapple go评论57阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月15日 16:24:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定