spring引导回滚

wxclj1h5  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(317)

这个问题在这里已经有答案了

注解@事务性。如何回滚(4个答案)
三个月前关门了。
我正在开发一个springboot应用程序,它需要在抛出错误时回滚所有数据库事务。例如,在这里,我与学生创建一个文件,当我给它一个未知的路径时,我有一个filenotfoundexception,但在数据库中的保存是提交的。我被要求回滚每一个技术错误提交的事务。有什么帮助吗?
我的代码示例:

@Override
public boolean registerStudent(Student student){
//some code
student.setStatus("registred");
studentService.save(student);
createFile(student);
//return
}

private void createFile(Student student) throws IOException{
try {
//code to create file
} catch (IOException ex) {
//log the error
}
}
8ehkhllq

8ehkhllq1#

您正在捕获异常,因此不可能回滚。
除此之外,registerstudent方法必须与studentservice.save共享事务(例如,在两个方法上都使用@transactional),否则studentservice将把它保存在一个独立的事务中,即使抛出异常也不会回滚它

相关问题