Java源码示例:com.codingapi.txlcn.tc.annotation.LcnTransaction

示例1
@LcnTransaction
@Transactional(rollbackFor = Exception.class)
@Override
public String execute(String value, String exFlag, String flag) {
    String dResp = serviceBClient.rpc(value);
    // step2. call remote ServiceB
    String eResp = serviceCClient.rpc(value);
    // step3. execute local transaction
    Demo demo = new Demo();
    demo.setGroupId(TracingContext.tracing().groupId());
    demo.setDemoField(value);
    demo.setCreateTime(new Date());
    demo.setAppName(Transactions.getApplicationId());
    demoMapper.save(demo);

    // 置异常标志,DTX 回滚
    if (Objects.nonNull(exFlag)) {
        throw new IllegalStateException("by exFlag");
    }

    return dResp + " > " + eResp + " > " + "ok-service-a";
}
 
示例2
@LcnTransaction//分布式事务
@Override
public String txlcn(String exFlag) {
    //先调用本地服务,新增一个用户user PS:调用EntityManager的merge,传进去的实体字段是什么就保存什么
    TbUser tbUser = new TbUser();
    tbUser.setUsername("huanzi");
    tbUser.setPassword("123456");
    TbUser user = entityManager.merge(tbUser);
    System.out.println(user);

    //调用B服务,新增一个用户描述description
    TbDescription description1 = bFeign.txlcn(user.getId());
    System.out.println(description1);

    //调用C服务,再新增一个用户描述description
    TbDescription description2 = cFeign.txlcn(user.getId());
    System.out.println(description2);

    //根据标识,是否抛出异常
    if (StringUtils.isEmpty(exFlag)) {
        return "操作成功,请查看一下数据库验证!";
    } else {
        throw new RuntimeException("rollback transactional by exFlag");
    }
}
 
示例3
@Override
@LcnTransaction
public void packageAndSend(TradeLog tradeLog) {
    TradeLog tl = new TradeLog();
    tl.setGoodsId(tradeLog.getGoodsId());
    tl.setGoodsName(tradeLog.getGoodsName());
    tl.setStatus("打包完毕,开始物流配送!");
    tl.setCreateTime(new Date());

    this.save(tl);
    log.info("商品ID为{},名称为{}的商品打包完毕,开始物流配送", tradeLog.getGoodsId(), tradeLog.getGoodsName());

    hashMap.put(TracingContext.tracing().groupId(), tradeLog.getId());
}
 
示例4
@Override
@LcnTransaction
public void orderAndPay(TradeLog tradeLog) {
    tradeLog.setCreateTime(new Date());
    tradeLog.setStatus("下单并支付成功");

    // 保存支付日志
    this.save(tradeLog);
    log.info("用户已经下单并支付成功商品ID为{},名称为{}的商品", tradeLog.getGoodsId(), tradeLog.getGoodsName());
    // 调用远程方法,打包并配送商品
    remoteTradeLogService.packageAndSend(tradeLog);
    // throw new RuntimeException("抛个异常,测试全局回滚");
}
 
示例5
@Override
@LcnTransaction
@Transactional(rollbackFor = Exception.class)
public String rpc(String value) {
    Demo demo = new Demo();
    demo.setGroupId(TracingContext.tracing().groupId());
    demo.setDemoField(value);
    demo.setAppName(Transactions.getApplicationId());
    demo.setCreateTime(new Date());
    demoMapper.save(demo);
    return "ok-service-b";
}
 
示例6
@Override
@LcnTransaction
@Transactional(rollbackFor = Exception.class)
public String rpc(String value) {
    Demo demo = new Demo();
    demo.setDemoField(value);
    demo.setCreateTime(new Date());
    demo.setAppName(Transactions.getApplicationId());
    demo.setGroupId(TracingContext.tracing().groupId());
    demoMapper.save(demo);
    if ("456".equals(value)) {
        throw new IllegalStateException("by test");
    }
    return "ok-service-c";
}
 
示例7
@LcnTransaction
@Override
public void saveAndPayOrder(String productName) {

    FwTradeLog fwTradeLog =new FwTradeLog(StatusEnum.TWO);
    fwTradeLog.setProductId(System.currentTimeMillis());
    fwTradeLog.setProductName(productName);
    fwTradeLogService.save(fwTradeLog);
    log.info("[订单状态{}]=>{},当前商品id=>{},商品名称=>{}",fwTradeLog.getOrderId(), StatusEnum.TWO.getDesc(),fwTradeLog.getProductId(),fwTradeLog.getProductName());

    remoteSendServiceFeign.sendOrder(fwTradeLog);

    int i=1/0;
}
 
示例8
@LcnTransaction
@Override
public void sendOrder(FwTradeLog fwTradeLog) {
    fwTradeLog.setStatus(StatusEnum.THREE.getValue());
    fwTradeLog.setStatusDsc(StatusEnum.THREE.getDesc());
    fwTradeLogService.save(fwTradeLog);
    log.info("[订单状态{}]=>{},当前商品id=>{},商品名称=>{}",fwTradeLog.getOrderId(), StatusEnum.THREE.getDesc(),fwTradeLog.getProductId(),fwTradeLog.getProductName());
}
 
示例9
@LcnTransaction//分布式事务
@Override
public TbDescription txlcn(Integer userId) {
    TbDescription tbDescription = new TbDescription();
    tbDescription.setUserId(userId);
    tbDescription.setDescription("服务B设置的描述");
    return entityManager.merge(tbDescription);
}
 
示例10
@LcnTransaction//分布式事务
@Override
public TbDescription txlcn(Integer userId) {
    TbDescription tbDescription = new TbDescription();
    tbDescription.setUserId(userId);
    tbDescription.setDescription("服务B设置的描述");
    return entityManager.merge(tbDescription);
}
 
示例11
@LcnTransaction//分布式事务
@Override
public TbDescription txlcn(Integer userId) {
    TbDescription tbDescription = new TbDescription();
    tbDescription.setUserId(userId);
    tbDescription.setDescription("服务C设置的描述");
    return entityManager.merge(tbDescription);
}