我的控制器如下: @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)來避免。
添加回答
舉報
0/150
提交
取消