{"record":{"id":"0ab683d2733fd40d","repo":"alibaba/spring-cloud-alibaba","slug":"no-enough-stock","errorCode":null,"errorMessage":"no enough stock","messagePattern":"no enough stock","errorType":"exception","errorClass":"BusinessException","httpStatus":null,"severity":"error","filePath":"spring-cloud-alibaba-examples/integrated-example/integrated-storage/src/main/java/com/alibaba/cloud/integration/storage/service/impl/StorageServiceImpl.java","lineNumber":72,"sourceCode":"\t\tif (updateCount == 0) {\n\t\t\tthrow new BusinessException(\"deduct stock failed\");\n\t\t}\n\t}\n\n\t@Override\n\tpublic Result<?> getRemainCount(String commodityCode) {\n\t\tInteger stock = storageMapper.getStock(commodityCode);\n\t\tif (stock == null) {\n\t\t\treturn Result.failed(\"commodityCode wrong,please check commodity code\");\n\t\t}\n\t\treturn Result.success(stock);\n\t}\n\n\tprivate void checkStock(String commodityCode, Integer count)\n\t\t\tthrows BusinessException {\n\t\tInteger stock = storageMapper.getStock(commodityCode);\n\t\tif (stock < count) {\n\t\t\tthrow new BusinessException(\"no enough stock\");\n\t\t}\n\t}\n\n}\n","sourceCodeStart":54,"sourceCodeEnd":77,"githubUrl":"https://github.com/alibaba/spring-cloud-alibaba/blob/115d5901102009492e05d5ec18c3f79cad4077d0/spring-cloud-alibaba-examples/integrated-example/integrated-storage/src/main/java/com/alibaba/cloud/integration/storage/service/impl/StorageServiceImpl.java#L54-L77","documentation":"Thrown by the private checkStock helper inside StorageServiceImpl.reduceStock. It reads stock via storageMapper.getStock(commodityCode) and throws BusinessException(\"no enough stock\") when stock < count. This is the pre-flight stock check before the deducting UPDATE. Because the UPDATE has no count-guard, this Java-side check is the only thing preventing stock from going negative. Note: getStock returns null if the commodityCode is absent, and `stock < count` would then throw NullPointerException rather than this message.","triggerScenarios":"reduceStock(commodityCode, count) called with count greater than the storage row's count. Reached when the order orchestrator deducts stock for an order whose quantity exceeds available units.","commonSituations":"Storage seeded with fewer units than requested; prior orders exhausted a commodity; a demo test ordering more than the seeded count.","solutions":["Check the storage count: SELECT count FROM storage WHERE commodity_code = '<commodityCode>'.","Replenish: UPDATE storage SET count = <units> WHERE commodity_code = '<commodityCode>'.","Reduce the requested count to fit available stock."],"exampleFix":"// before\nprivate void checkStock(String commodityCode, Integer count) throws BusinessException {\n    Integer stock = storageMapper.getStock(commodityCode);\n    if (stock < count) {\n        throw new BusinessException(\"no enough stock\");\n    }\n}\n// after: guard null to avoid an NPE\nprivate void checkStock(String commodityCode, Integer count) throws BusinessException {\n    Integer stock = storageMapper.getStock(commodityCode);\n    if (stock == null) {\n        throw new BusinessException(\"commodity not found: \" + commodityCode);\n    }\n    if (stock < count) {\n        throw new BusinessException(\"no enough stock\");\n    }\n}","handlingStrategy":"validation","validationCode":"// Pre-check stock and guard null to avoid an NPE.\nInteger stock = storageMapper.getStock(commodityCode);\nif (stock == null) {\n    throw new BusinessException(\"commodity not found: \" + commodityCode);\n}\nif (stock < count) {\n    throw new BusinessException(\"no enough stock\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Null-check Integer mapper results before comparison.","Seed stock above the maximum count you will request.","Note the UPDATE has no count guard, so this Java check is the only floor."],"tags":["seata","distributed-transaction","business-rule","stock","validation"],"backgroundTag":null,"analyzedSha":"115d5901102009492e05d5ec18c3f79cad4077d0","analyzedAt":"2026-08-14T04:47:13.900Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}