春季交易回滚问题
在回滚保存数据时面对问题。
问题: 在我的Java方法中,有三个储蓄动作。数据保存到其他表。我想要保存失败时,所有保存的交易将是回滚。
例如,有三个表由主表和子表组成。所有表保存在方法中。 apvoucherentrydetails是子表,当它保存失败时,上面的交易应该是回滚。
我该怎么做?
提前致谢
@Override
public ApiResponse saveVoucher(APVoucherBatch batch) {
APVoucherBatch savedBatch = voucherBatchRepo.save(batch); //table 1
//==========save entry=============
List<APVoucherEntry> entryList = batch.getEntryList();
for(APVoucherEntry info: entryList){
info.setVoucherBatch(savedBatch);
info.setBatchNo(savedBatch.getBatchNo());
APVoucherEntry savedEntry = entryRepo.save(info); //table 2
//==========save detail=============
List<APVoucherEntryDetails> detailsList = info.getDetailsList();
for(APVoucherEntryDetails dtl: detailsList){
dtl.setBatchList(savedBatch);
dtl.setEntryInfo(savedEntry);
detailsRepo.save(dtl); //table 3
}
}
return new ApiResponse();
}
Facing an issue while rollback saving data.
Problem:
In my java method there are three saving actions. Data save to different table. I want when a saving fails then all saved transaction will be rollback.
Example, there are three tables consist of master table and sub table. All table save in a method. APVoucherEntryDetails is the child table, when it saves fail then upper transactions should be rollback.
How can I do it ?
Thanks in advance
@Override
public ApiResponse saveVoucher(APVoucherBatch batch) {
APVoucherBatch savedBatch = voucherBatchRepo.save(batch); //table 1
//==========save entry=============
List<APVoucherEntry> entryList = batch.getEntryList();
for(APVoucherEntry info: entryList){
info.setVoucherBatch(savedBatch);
info.setBatchNo(savedBatch.getBatchNo());
APVoucherEntry savedEntry = entryRepo.save(info); //table 2
//==========save detail=============
List<APVoucherEntryDetails> detailsList = info.getDetailsList();
for(APVoucherEntryDetails dtl: detailsList){
dtl.setBatchList(savedBatch);
dtl.setEntryInfo(savedEntry);
detailsRepo.save(dtl); //table 3
}
}
return new ApiResponse();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论