mogodb开启事务后先查询后修会报错

@GetMapping("/update/{studentId}")
    @Transactional(rollbackFor = Exception.class)
    public void update(@PathVariable String studentId) throws Exception{
       //查询学生
       //修改学生
        // 报错 Cannot recover the transaction decision without a recoveryToken
    }

提供参考实例【golang操作mongodb的驱动mongo-go-driver的事务支持和访问控制】,链接:https://blog.csdn.net/sdghchj/article/details/85249392?spm=1001.2101.3001.6650.16&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-16-85249392-blog-108727027.pc_relevant_3mothn_strategy_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-16-85249392-blog-108727027.pc_relevant_3mothn_strategy_recovery&utm_relevant_index=24

https://zhuanlan.zhihu.com/p/405898188

在开启事务的方法中,如果先执行了数据库查询操作,会导致事务无法回滚。这是因为在开启事务之后,事务管理器会为当前事务生成一个回滚标识(recovery token)。在事务提交或回滚之前,这个标识是无效的。但是如果在开启事务之后,事务中的某个操作已经执行了数据库查询,那么这个回滚标识就会失效,事务就无法回滚。

为了避免这个问题,可以把查询和更新操作放到同一个方法中,或者把查询操作放到另一一个方法中,再调用这个方法来获取数据。

例如可以把查询操作放到另一个方法中,然后调用这个方法来获取数据:

@Service public class StudentService
{
    @Transactional(rollbackFor = Exception.class) public void update(String studentId) throws Exception
    {
        Student student = getStudentById(studentId);
        // 修改学生
    }

public
    Student getStudentById(String studentId)
    {
        // 查询学生
    }
}