Search in sources :

Example 1 with GlobalTransactional

use of io.seata.spring.annotation.GlobalTransactional in project spring-cloud-alibaba by alibaba.

the class HomeController method rest.

@GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
@GetMapping(value = "/seata/rest", produces = "application/json")
public String rest() {
    String result = restTemplate.getForObject("http://127.0.0.1:18082/storage/" + COMMODITY_CODE + "/" + ORDER_COUNT, String.class);
    if (!SUCCESS.equals(result)) {
        throw new RuntimeException();
    }
    String url = "http://127.0.0.1:18083/order";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("userId", USER_ID);
    map.add("commodityCode", COMMODITY_CODE);
    map.add("orderCount", ORDER_COUNT + "");
    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
    ResponseEntity<String> response;
    try {
        response = restTemplate.postForEntity(url, request, String.class);
    } catch (Exception exx) {
        throw new RuntimeException("mock error");
    }
    result = response.getBody();
    if (!SUCCESS.equals(result)) {
        throw new RuntimeException();
    }
    return SUCCESS;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) GlobalTransactional(io.seata.spring.annotation.GlobalTransactional) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with GlobalTransactional

use of io.seata.spring.annotation.GlobalTransactional in project hummer-framework by hummer-team.

the class OrderService method save.

@GlobalTransactional(timeoutMills = 30000, name = "save-order")
@TargetDataSourceTM(dbName = "hj_classs_courseware", transactionManager = "hj_classs_courseware_TM", timeout = 5)
public int save() {
    OrderPo po = new OrderPo();
    po.setOrderTitle("test order");
    po.setUserId(45677);
    int result = orderDaoMapper.save(po);
    log.info("save order done,db response {}", result);
    return po.getOrderId();
}
Also used : OrderPo(com.hummer.api.po.OrderPo) GlobalTransactional(io.seata.spring.annotation.GlobalTransactional) TargetDataSourceTM(com.hummer.dao.annotation.TargetDataSourceTM)

Example 3 with GlobalTransactional

use of io.seata.spring.annotation.GlobalTransactional in project SpringBoot-Project by MartinDai.

the class TradeService method createOrder.

/**
 * 创建订单
 */
@GlobalTransactional(timeoutMills = 30000)
public void createOrder(String goodsCode, int stockNum, boolean mockException) {
    String xid = RootContext.getXID();
    System.out.println("createOrder 当前正在执行的事务xid:" + xid);
    String result = storageService.reduceStock(goodsCode, stockNum);
    if (!SUCCESS_RESPONSE.equals(result)) {
        throw new IllegalStateException("库存扣减失败! xid:" + xid);
    }
    Order order = new Order().setGoodsCode(goodsCode).setStockNum(stockNum).setMoney(stockNum * 100).setUserId(1);
    result = orderService.createOrder(order);
    if (!SUCCESS_RESPONSE.equals(result)) {
        throw new IllegalStateException("创建订单失败! xid:" + xid);
    }
    if (mockException) {
        throw new IllegalStateException("模拟用户业务异常! xid:" + xid);
    }
}
Also used : Order(com.doodl6.springboot.seata.common.entity.Order) GlobalTransactional(io.seata.spring.annotation.GlobalTransactional)

Example 4 with GlobalTransactional

use of io.seata.spring.annotation.GlobalTransactional in project JavaFamily by chenjiabing666.

the class OrderServiceImpl method create.

/**
 * 下订单的业务接口,其中完成了扣减库存-》扣减余额-》创建订单的流程
 * 使用@GlobalTransactional这个注解开启一个全局的分布式事务
 * @param userId  用于唯一Id
 * @param productId  商品Id
 * @param num  购买数量
 */
@GlobalTransactional
@Override
public Order create(String userId, Long productId, Long num) {
    // todo 模拟下业务逻辑,细致的逻辑自己完善
    // 1. 扣减库存
    log.info("扣减库存开始..............");
    storageFeignService.deduct(productId, num);
    log.info("扣减库存结束..............");
    // 2. 扣余额
    log.info("扣减余额开始..............");
    Storage storage = storageFeignService.getById(productId).getData();
    accountFeignService.deduct(userId, storage.getPrice() * num);
    log.info("扣减余额结束..............");
    // 3. 创建订单
    log.info("创建订单开始..............");
    Order order = Order.builder().userId(userId).productId(productId).num(num).status(2).createTime(new Date()).build();
    Order save = orderRepository.save(order);
    return save;
}
Also used : Order(cn.myjszl.model.Order) Storage(cn.myjszl.model.Storage) Date(java.util.Date) GlobalTransactional(io.seata.spring.annotation.GlobalTransactional)

Example 5 with GlobalTransactional

use of io.seata.spring.annotation.GlobalTransactional in project FCLProject by FlowingCloudL.

the class OrderServiceImpl method submitOrder.

@GlobalTransactional
@Override
public Long submitOrder(Long uid, OrderSubmitDTO orderSubmitDTO) {
    // 扣减库存
    List<StockModifyBO> bos = orderSubmitDTO.getSkus().stream().map(dto -> {
        StockModifyBO bo = new StockModifyBO();
        BeanUtils.copyProperties(dto, bo);
        return bo;
    }).collect(Collectors.toList());
    remoteStockService.decrStock(bos);
    // 创建订单基本信息
    OrderInfoEntity orderInfoEntity = new OrderInfoEntity();
    BeanUtils.copyProperties(orderSubmitDTO, orderInfoEntity);
    orderInfoEntity.setUserId(uid);
    orderInfoEntity.setCreateTime(LocalDateTime.now());
    orderInfoEntity.setUpdateTime(LocalDateTime.now());
    orderInfoMapper.insert(orderInfoEntity);
    // 创建订单项
    for (OrderSubmitSkuDTO submitSkuDTO : orderSubmitDTO.getSkus()) {
        // 远程调用获得订单项所需的SKU和SPU信息
        OrderSpuBO orderSpuBO = remoteSpuService.getSpuBySkuId(submitSkuDTO.getSkuId());
        // 生成订单项Entity
        OrderItemEntity orderItemEntity = new OrderItemEntity();
        // 设置订单id
        orderItemEntity.setOrderId(orderInfoEntity.getOrderId());
        // 设置spu部分
        BeanUtils.copyProperties(orderSpuBO, orderItemEntity);
        // 设置sku部分
        BeanUtils.copyProperties(orderSpuBO.getSkuBO(), orderItemEntity);
        // 设置单价
        orderItemEntity.setSkuPrice(orderSpuBO.getSkuBO().getPrice());
        // 设置数量
        orderItemEntity.setSkuQuantity(submitSkuDTO.getCount());
        // 计算订单项价格
        orderItemEntity.setRealAmount(orderItemEntity.getSkuPrice().multiply(new BigDecimal(orderItemEntity.getSkuQuantity())));
        orderItemMapper.insert(orderItemEntity);
    }
    return null;
}
Also used : OrderSubmitSkuDTO(com.fp.mall.order.model.dto.OrderSubmitSkuDTO) OrderService(com.fp.mall.order.service.OrderService) OrderInfoMapper(com.fp.mall.order.mapper.OrderInfoMapper) OrderItemDTO(com.fp.mall.order.model.dto.OrderItemDTO) Resource(javax.annotation.Resource) LocalDateTime(java.time.LocalDateTime) StockModifyBO(com.fp.api.mall.product.model.StockModifyBO) RemoteSpuService(com.fp.api.mall.product.service.RemoteSpuService) RemoteStockService(com.fp.api.mall.product.service.RemoteStockService) OrderSpuBO(com.fp.api.mall.product.model.OrderSpuBO) Collectors(java.util.stream.Collectors) OrderItemEntity(com.fp.mall.order.model.entity.OrderItemEntity) OrderSubmitDTO(com.fp.mall.order.model.dto.OrderSubmitDTO) BigDecimal(java.math.BigDecimal) List(java.util.List) GlobalTransactional(io.seata.spring.annotation.GlobalTransactional) Service(org.springframework.stereotype.Service) OrderItemMapper(com.fp.mall.order.mapper.OrderItemMapper) DubboReference(org.apache.dubbo.config.annotation.DubboReference) OrderInfoEntity(com.fp.mall.order.model.entity.OrderInfoEntity) BeanUtils(org.springframework.beans.BeanUtils) Transactional(org.springframework.transaction.annotation.Transactional) OrderSubmitSkuDTO(com.fp.mall.order.model.dto.OrderSubmitSkuDTO) StockModifyBO(com.fp.api.mall.product.model.StockModifyBO) OrderInfoEntity(com.fp.mall.order.model.entity.OrderInfoEntity) OrderSpuBO(com.fp.api.mall.product.model.OrderSpuBO) OrderItemEntity(com.fp.mall.order.model.entity.OrderItemEntity) BigDecimal(java.math.BigDecimal) GlobalTransactional(io.seata.spring.annotation.GlobalTransactional)

Aggregations

GlobalTransactional (io.seata.spring.annotation.GlobalTransactional)19 Transactional (org.springframework.transaction.annotation.Transactional)4 List (java.util.List)3 HttpEntity (org.springframework.http.HttpEntity)3 HttpHeaders (org.springframework.http.HttpHeaders)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 MultiValueMap (org.springframework.util.MultiValueMap)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 DS (com.baomidou.dynamic.datasource.annotation.DS)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Order (cn.myjszl.model.Order)1 Storage (cn.myjszl.model.Storage)1 ProductOutput (com.amusing.start.client.output.ProductOutput)1 UserAccountOutput (com.amusing.start.client.output.UserAccountOutput)1 OrderException (com.amusing.start.order.exception.OrderException)1 Order (com.baomidou.samples.seata.entity.Order)1