java 事务提交(批量处理数据,单个批次执行完成后直接提交事务)

方法一:接口+REQUIRES_NEW 实现单个事务提交

方式1:
for (TIrBuPBom buPBom : batchList) {
   // 查询待处理的批次数据
  List<TIrBuPBom> pBomList = pBomMapperBase.list(new LambdaQueryWrapper<TIrBuPBom>()
                        .eq(TIrBuPBom::getBatchNo, buPBom.getBatchNo())
                        .eq(TIrBuPBom::getCarTypeCode, buPBom.getCarTypeCode())
                        .eq(TIrBuPBom::getDealStatus, "0"));
if (pBomList.size() == buPBom.getBatchSl()) {
	// 通过接口+ @Transactional(propagation = Propagation.REQUIRES_NEW)实现,单个事务提交
 	itIrBuPBomBiz.dealBomBatch(buPBom, pBomList, plantListMap, wkShopListMap, userName, now, tenancyId);
} else {
	 break;
}
}

 @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void dealBomBatch(TIrBuPBom buPBom, List<TIrBuPBom> pBomList, Map<String, List<Map<String, String>>> plantListMap, Map<String, List<Map<String, String>>> wkShopListMap, String userName, LocalDateTime now, String tenancyId) {

....}

方法二:手动提交

 if (pBomList.size() == buPBom.getBatchSl()) {
                    // 手动提交事务
                    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
                    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
                    def.setIsolationLevel(2);
                    TransactionStatus status = null;
                    try {
                        status = this.platformTransactionManager.getTransaction(def);
                    this.dealBomBatch(buPBom, pBomList, plantListMap, wkShopListMap, userName, now, tenancyId);
                        this.platformTransactionManager.commit(status);
                    } catch (Exception var18) {
                        this.platformTransactionManager.rollback(status);
                        throw new BusicenException(var18.toString());
                    }

                } else {
                    break;
                }

你可能感兴趣的:(java)