限购-并发

  @TargetDataSource(name = "mysql2")
    @Transactional
    public void test() {
        Integer goodsNum = exampleMapper.selectGoodsNum();
        Integer hasOrderNum = exampleMapper.selectOrderNum();
        try {
            if (goodsNum > 0 && hasOrderNum < 3) {
                Integer reduce = exampleMapper.reduceGoods();
                logger.warn("===reduce num:" + reduce);
                if (reduce == 1) {
                    hasOrderNum = exampleMapper.selectOrderNumLock();
                    logger.warn("===order hasOrderNum:" + hasOrderNum);
                    if (hasOrderNum >= 3) {
                        throw new TokenException(Constants.OTHER_ERROR, "限购3件");
                    }
                    Integer order = exampleMapper.insertOrder();
                    logger.warn("===order order:" + order);
                }
            } else {
                System.out.println("====== 条件不符合:{}=== goodsNum:{}" + goodsNum + " hasOrderNum:" + hasOrderNum);
            }

        } catch (TokenException tok) {
            throw tok;
        } catch (Exception e) {
            System.out.println("===异常===" + JsonUtil.bean2Json(e));
            throw new RuntimeException(e);
        }
    }
 
public class TokenException extends RuntimeException {
    public String msg;
    public Integer code;

    public TokenException(Integer code, String msg) {
        this.msg = msg;
        this.code = code;
    }
}

 

你可能感兴趣的:(限购-并发)