扩展
公共类-AssertUtil
import cn.hutool.core.util.StrUtil;import com.luoma.finance.excepiton.InternalApiException;/**** 断言工具类*/public class AssertUtil {/*** 断言* @param flag 断言标识* @param code 异常码* @param message* 异常信息,* 如果异常信息需要多语言统一{@link com.luoma.finance.handler.GlobalExceptionHandler}中做多语言处理,* 以message 作为key去获取多语言信息,* 如果需要支持带动态参数的多语言需要拓展 {@link com.luoma.finance.excepiton.InternalApiException}* @param messageParam 异常信息参数*/public static void isTrue(boolean flag,int code,String message,Object... messageParam){if(!flag){throw new InternalApiException(code, StrUtil.format(message,messageParam));}}}
第一种情况,入口没有加 @Transactional,TransactionTemplate 里面报错
@ResourceIPaymentWalletAccountService accountService;@Resourceprivate TransactionTemplate transactionTemplate;@GetMapping("/testPaymentWalletAccount")public PaymentWalletAccount testPaymentWalletAccount() {PaymentWalletAccount dto = new PaymentWalletAccount();dto.setSourceSystem("FPP");dto.setSourceNum("FPP1");dto.setReferenceId("FPP1");dto.setRechargeOrderNumber("1");dto.setPayOrderNumber("1");dto.setOrgCode("101");dto.setOrgName("OU_101");dto.setPayCurrencyCode("CNY");dto.setPayAmount(new BigDecimal("2.22"));dto.setIsReconciliation("Y");dto.setReconciliationStatus("FAILED");dto.setActualPayDate(CommonUtil.getDataStr(LocalDateTime.now(), null));dto.setActualPayMonth("202403");dto.setPayMonth("202404");int count = accountService.getBaseMapper().insert(dto);String methodName = "testPaymentWalletAccount() >> ";log.info(methodName + "count:{}", count);transactionTemplate.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {PaymentWalletAccount dto = new PaymentWalletAccount();dto.setSourceSystem("FPP");dto.setSourceNum("FPP");dto.setReferenceId(UUID.randomUUID().toString());dto.setRechargeOrderNumber("1");dto.setPayOrderNumber("1");dto.setOrgCode("101");dto.setOrgName("OU_101");dto.setPayCurrencyCode("CNY");dto.setPayAmount(new BigDecimal("2.22"));dto.setIsReconciliation("Y");dto.setReconciliationStatus("FAILED");dto.setActualPayDate(CommonUtil.getDataStr(LocalDateTime.now(), null));dto.setActualPayMonth("202403");dto.setPayMonth("202404");int count = accountService.getBaseMapper().insert(dto);String methodName = "testPaymentWalletAccount() >> ";log.info(methodName + "count:{}", count);//故意设置失败count = 0;AssertUtil.isTrue(count > 0, ResponseCode.UNKNOWN_EXCEPTION.value(),"insert 数据失败");}});return dto;}
- 调用接口,查询数据库,添加了外层的数据
第二种情况,入口加 @Transactional,TransactionTemplate 里面报错
@ResourceIPaymentWalletAccountService accountService;@Resourceprivate TransactionTemplate transactionTemplate;@GetMapping("/testPaymentWalletAccount")@Transactional(rollbackFor = Exception.class)public PaymentWalletAccount testPaymentWalletAccount() {PaymentWalletAccount dto = new PaymentWalletAccount();dto.setSourceSystem("FPP");dto.setSourceNum("FPP1");dto.setReferenceId("FPP1");dto.setRechargeOrderNumber("1");dto.setPayOrderNumber("1");dto.setOrgCode("101");dto.setOrgName("OU_101");dto.setPayCurrencyCode("CNY");dto.setPayAmount(new BigDecimal("2.22"));dto.setIsReconciliation("Y");dto.setReconciliationStatus("FAILED");dto.setActualPayDate(CommonUtil.getDataStr(LocalDateTime.now(), null));dto.setActualPayMonth("202403");dto.setPayMonth("202404");int count = accountService.getBaseMapper().insert(dto);String methodName = "testPaymentWalletAccount() >> ";log.info(methodName + "count:{}", count);transactionTemplate.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {PaymentWalletAccount dto = new PaymentWalletAccount();dto.setSourceSystem("FPP");dto.setSourceNum("FPP");dto.setReferenceId(UUID.randomUUID().toString());dto.setRechargeOrderNumber("1");dto.setPayOrderNumber("1");dto.setOrgCode("101");dto.setOrgName("OU_101");dto.setPayCurrencyCode("CNY");dto.setPayAmount(new BigDecimal("2.22"));dto.setIsReconciliation("Y");dto.setReconciliationStatus("FAILED");dto.setActualPayDate(CommonUtil.getDataStr(LocalDateTime.now(), null));dto.setActualPayMonth("202403");dto.setPayMonth("202404");int count = accountService.getBaseMapper().insert(dto);String methodName = "testPaymentWalletAccount() >> ";log.info(methodName + "count:{}", count);//故意设置失败count = 0;AssertUtil.isTrue(count > 0, ResponseCode.UNKNOWN_EXCEPTION.value(),"insert 数据失败");}});return dto;}
- 调用接口,查询数据库,无数据
第三种情况,入口加 @Transactional,外面报错
@ResourceIPaymentWalletAccountService accountService;@Resourceprivate TransactionTemplate transactionTemplate;@GetMapping("/testPaymentWalletAccount")@Transactional(rollbackFor = Exception.class)public PaymentWalletAccount testPaymentWalletAccount() {PaymentWalletAccount dto = new PaymentWalletAccount();dto.setSourceSystem("FPP");dto.setSourceNum("FPP1");dto.setReferenceId("FPP1");dto.setRechargeOrderNumber("1");dto.setPayOrderNumber("1");dto.setOrgCode("101");dto.setOrgName("OU_101");dto.setPayCurrencyCode("CNY");dto.setPayAmount(new BigDecimal("2.22"));dto.setIsReconciliation("Y");dto.setReconciliationStatus("FAILED");dto.setActualPayDate(CommonUtil.getDataStr(LocalDateTime.now(), null));dto.setActualPayMonth("202403");dto.setPayMonth("202404");int count = accountService.getBaseMapper().insert(dto);String methodName = "testPaymentWalletAccount() >> ";log.info(methodName + "count:{}", count);transactionTemplate.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {PaymentWalletAccount dto = new PaymentWalletAccount();dto.setSourceSystem("FPP");dto.setSourceNum("FPP");dto.setReferenceId(UUID.randomUUID().toString());dto.setRechargeOrderNumber("1");dto.setPayOrderNumber("1");dto.setOrgCode("101");dto.setOrgName("OU_101");dto.setPayCurrencyCode("CNY");dto.setPayAmount(new BigDecimal("2.22"));dto.setIsReconciliation("Y");dto.setReconciliationStatus("FAILED");dto.setActualPayDate(CommonUtil.getDataStr(LocalDateTime.now(), null));dto.setActualPayMonth("202403");dto.setPayMonth("202404");int count = accountService.getBaseMapper().insert(dto);String methodName = "testPaymentWalletAccount() >> ";log.info(methodName + "count:{}", count);//故意设置失败//count = 0;AssertUtil.isTrue(count > 0, ResponseCode.UNKNOWN_EXCEPTION.value(),"insert 数据失败");}});AssertUtil.isTrue(false, ResponseCode.UNKNOWN_EXCEPTION.value(),"故意抛出异常");return dto;}
- 调用接口,查询数据库,无数据
第四种情况,入口不加 @Transactional,外面报错
@ResourceIPaymentWalletAccountService accountService;@Resourceprivate TransactionTemplate transactionTemplate;@GetMapping("/testPaymentWalletAccount")public PaymentWalletAccount testPaymentWalletAccount() {PaymentWalletAccount dto = new PaymentWalletAccount();dto.setSourceSystem("FPP");dto.setSourceNum("FPP1");dto.setReferenceId("FPP1");dto.setRechargeOrderNumber("1");dto.setPayOrderNumber("1");dto.setOrgCode("101");dto.setOrgName("OU_101");dto.setPayCurrencyCode("CNY");dto.setPayAmount(new BigDecimal("2.22"));dto.setIsReconciliation("Y");dto.setReconciliationStatus("FAILED");dto.setActualPayDate(CommonUtil.getDataStr(LocalDateTime.now(), null));dto.setActualPayMonth("202403");dto.setPayMonth("202404");int count = accountService.getBaseMapper().insert(dto);String methodName = "testPaymentWalletAccount() >> ";log.info(methodName + "count:{}", count);transactionTemplate.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {PaymentWalletAccount dto = new PaymentWalletAccount();dto.setSourceSystem("FPP");dto.setSourceNum("FPP");dto.setReferenceId(UUID.randomUUID().toString());dto.setRechargeOrderNumber("1");dto.setPayOrderNumber("1");dto.setOrgCode("101");dto.setOrgName("OU_101");dto.setPayCurrencyCode("CNY");dto.setPayAmount(new BigDecimal("2.22"));dto.setIsReconciliation("Y");dto.setReconciliationStatus("FAILED");dto.setActualPayDate(CommonUtil.getDataStr(LocalDateTime.now(), null));dto.setActualPayMonth("202403");dto.setPayMonth("202404");int count = accountService.getBaseMapper().insert(dto);String methodName = "testPaymentWalletAccount() >> ";log.info(methodName + "count:{}", count);//故意设置失败//count = 0;AssertUtil.isTrue(count > 0, ResponseCode.UNKNOWN_EXCEPTION.value(),"insert 数据失败");}});AssertUtil.isTrue(false, ResponseCode.UNKNOWN_EXCEPTION.value(),"故意抛出异常");return dto;}
- 调用接口,查询数据库,有两条数据,外部错误不影响 transactionTemplate 内部事务