1. 程式人生 > >Spring 實現部分事務回滾

Spring 實現部分事務回滾

light back true prop 回滾 sage .class lba aaa

例如有業務需求,在catch異常後,catch塊內把異常的信息存入到數據庫,而catch外的數據全部回滾

try {
  .......
   aaaService.save();
 
}catch(RuntimeException e) {

    bbbService.save(e.getMessage());
    throw new RuntimeException(e.getMessage());
}

 確保aaaService.save()的數據回滾,而 bbbService的save不回滾。

只能在bbbService save的頭部加上開啟新的事務即可

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = Exception.class)

Spring 實現部分事務回滾