【问题标题】:spring boot - rollback on an oracle database [duplicate]spring boot - 在oracle数据库上回滚[重复]
【发布时间】:2021-05-03 19:47:25
【问题描述】:

我正在开发一个 Spring Boot 应用程序,该应用程序需要在引发错误时回滚所有数据库事务。例如,我正在与学生创建一个文件,当我给它一个未知的路径时,我有一个 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
}
}

【问题讨论】:

    标签: java spring oracle spring-boot spring-transactions


    【解决方案1】:

    您正在捕获异常,因此无法回滚。

    除此之外,registerStudent 方法必须与 studentService.save 共享事务(例如在两个方法上都使用@Transactional),否则 studentService 会将其保存在隔离事务中,即使您抛出异常也不会回滚它

    【讨论】:

      猜你喜欢
      • 2021-08-28
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2017-07-22
      • 2015-03-19
      • 2021-04-14
      相关资源
      最近更新 更多