亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

SpringBoot中的TransactionRequiredException

SpringBoot中的TransactionRequiredException

慕村225694 2023-09-06 15:47:38
我的控制器如下: @GetMapping("/groupByCourse")    public Reply getStudentsCountByCourse(){        Reply reply=new Reply();        reply.setData(service.getStudentsCountByCourse());        return reply;    }服務是:-public List getStudentsCountByCourse()  {            List students=new ArrayList<>();            students=studentCourseRepo.findCountStudentByCourse();            getCourseCountByStudent();        return students;    }    @Transactional(Transactional.TxType.REQUIRES_NEW)   public List getCourseCountByStudent()  {            List students=new ArrayList<>();            students=studentCourseRepo.findCountCourseByStudent();        return students;    }存儲庫是:-@Repositorypublic interface StudentCourseRepo  extends CrudRepository<StudentCourseTbl, StudentCourseTblPK> {    @Query(value = "select  sc.studentCourseTblPK.courseId,count(sc.studentCourseTblPK.studentId) from StudentCourseTbl sc group by sc.studentCourseTblPK.courseId")    List findCountStudentByCourse();    @Lock(LockModeType.PESSIMISTIC_WRITE)    @Query(value = "select  s.id,s.name,count(sc.studentCourseTblPK.courseId) from StudentCourseTbl sc  right join StudentsTbl s on sc.studentCourseTblPK.studentId=s.id group by s.id")    List findCountCourseByStudent();}我已經在我的服務方法中使用 @Transactional(Transactional.TxType.REQUIRES_NEW) 來使用 Pessimistic_write 鎖執行查詢,但即使在事務模式設置為需要新之后,我仍然收到TransactionRequiredException 。我知道我可以通過使用使代碼工作我的 getStudentsCountByCourse 方法上的 @Transactional 注釋,但我想知道它當前不起作用的原因。我正在使用 springboot 版本 2.1.7.Release 并使用 mysql 作為數據庫
查看完整描述

1 回答

?
四季花海

TA貢獻1811條經驗 獲得超5個贊

這是 Spring 默認 CGLIB 代理的已知限制。由于事務行為基于代理,因此目標對象的一個方法調用其另一個方法不會導致事務行為,即使后一個方法被標記為事務性的。

這可以通過在字節碼中編織事務行為而不是使用代理(即使用 AspectJ)來避免。

查看完整回答
反對 回復 2023-09-06
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號