java - 发生异常后如何丢弃hibernate/JPA中的更改?
问题描述
异常后如何丢弃更改?即使recordA保存失败,我也想保存recordB。
//The transaction comes from else where, cannot modify it
@Transactional
void updateRecord{
Record recordA = repository.findById(ida);
Record recordB = repository.findById(idb);
recordA.setXXX();
recordB.setXXX();
try {
repository.saveAndFlush(recordA);
} catch (Exception exp) {
if (exp instanceof DataIntegrityViolationException) {
entityManager.detach(recordA);
} else {
throw new RuntimeException(exp);
}
}
repository.saveAndFlush(recordB);//got exception cause recordA still engaged
}
解决方案
不能在同一事务中完成。
1:需要设置 globalRollbackOnParticipationFailure = true
2:通过适当的传播开始一个新事务,在该事务中做所有事情并回滚
推荐阅读
- google-cloud-platform - 查看 GCP Cloud Datastore 指标、跟踪、查询计划
- jquery-scrollify - jquery-scrollify 中允许哪些缓动函数?
- search - Netsuite 保存的交易搜索 - 在结果中获取汇总字段
- assembly - PIC 代码中的位置相关数据类型部分?
- java - Java如何删除监听器
- php - Codeigniter 3 - HMVC,Phil Sturgeon 模板库问题
- javascript - 映射组件时渲染条件路由
- svg - SVG 文件中的“命名空间前缀 Inkscape”错误
- windows - 批处理文件检查其他文件中的字符串
- r - 如何为大型调查数据集获取 svydb R 包以返回标准错误