ItemService抛出异常库存不足 但是OrderService没有回滚而是插入了数据
OrderServerImpl 的createOrder
上面注释了
@Transactional(propagation = Propagation.REQUIRED)
调用ItemService 的I减少temSpec的count的方法 这个方法也有
@Transactional(propagation = Propagation.REQUIRED)
像老师的代码一样在OrderService没有处理异常 我设置了这个stock是0 所以UPDATE count 是0
异常正确抛出控制台也能看到这个异常 但是在OrderService忽略了这个异常别的都处理了
我贴一下代码
@Transactional(propagation = Propagation.REQUIRED) @Override public boolean createOrder(SubmitOrderBO submitOrderBO) { Orders order = new Orders(); List<String> list = new ArrayList<>(); String[] specIds = submitOrderBO.getItemSpecIds().split(","); Date now = new Date(); String orderId = this.sid.nextShort(); int totalAmt = 0, actualAmt = 0; order.setId(orderId); order.setPayMethod(submitOrderBO.getPayMethod()); order.setUserId(submitOrderBO.getUserId()); if (!StringUtils.isBlank(submitOrderBO.getLeftMsg())) { order.setLeftMsg(submitOrderBO.getLeftMsg()); } UserAddress address = this.addressService.getUserAddressByUserIdAndAddressId(submitOrderBO.getUserId(), submitOrderBO.getAddressId()); if (!StringUtils.isBlank(address.getProvince())) { list.add(address.getProvince()); } if (!StringUtils.isBlank(address.getCity())) { list.add(address.getCity()); } if (!StringUtils.isBlank(address.getDistrict())) { list.add(address.getDistrict()); } if (!StringUtils.isBlank(address.getDetail())) { list.add(address.getDetail()); } StringBuilder sb = new StringBuilder(); for (int i=0, len=list.size(); i<len; i++) if (i == len - 1) sb.append(list.get(i)); else sb.append(list.get(i) + ", "); if (sb.length() > 0) order.setReceiverAddress(sb.toString()); if (!StringUtils.isBlank(address.getReceiver())) { order.setReceiverName(address.getReceiver()); } if (!StringUtils.isBlank(address.getMobile())) { order.setReceiverMobile(address.getMobile()); } OrderStatus orderStatus = new OrderStatus(); for (String specId : specIds) { OrderItems orderItems = new OrderItems(); ItemsSpec itemsSpec = this.itemsService.getItemsSpecByItemsSpecId(specId); if (itemsSpec != null) { Items items = this.itemsService.getItemsByItemsId(itemsSpec.getItemId()); if (items != null) { totalAmt += itemsSpec.getPriceNormal(); actualAmt += itemsSpec.getPriceDiscount(); orderItems.setBuyCounts(1); orderItems.setId(this.sid.nextShort()); orderItems.setItemId(itemsSpec.getItemId()); ItemsImg img = this.itemsService.getItemsImgMainByItemId(items.getId()); if (img != null) orderItems.setItemImg(img.getUrl()); orderItems.setItemName(items.getItemName()); orderItems.setItemSpecId(specId); orderItems.setItemSpecName(itemsSpec.getName()); orderItems.setOrderId(orderId); orderItems.setPrice(itemsSpec.getPriceDiscount()); this.orderItemsMapper.insert(orderItems); Map<String, Object> map = new HashMap<>(); map.put("specId", specId); map.put("amt", 1); this.itemsService.updateItemsSpecCount(map); } } } orderStatus.setCreatedTime(now); orderStatus.setOrderId(orderId); orderStatus.setOrderStatus(OrderStatusEnum.WAIT_PAY.type); this.orderStatusMapper.insert(orderStatus); // money order.setTotalAmount(totalAmt); order.setRealPayAmount(actualAmt); order.setPostAmount(0); order.setIsComment(YesOrNo.No.type); order.setIsDelete(YesOrNo.No.type); order.setCreatedTime(now); order.setUpdatedTime(now); return this.ordersMapper.insert(order) == 1; }
ItemService
@Transactional(propagation = Propagation.REQUIRED) @Override public void updateItemsSpecCount(Map<String, Object> map) { if (this.itemsMapperCustom.updateItemsSpecCount(map) != 1) { throw new RuntimeException("Invalid transaction due to low stock inventory"); } }
38
收起
正在回答
1回答
看着代码没啥问题呀,runtime,而且你也没有做其他的捕获处理是吧?
你看这样精简一下service,做简单一些的测试看看,serviceA保存到数据库,serviceB就直接抛出异常,测试看看,另起两个方法去试试
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星