{"record":{"id":"f6e62aee41738c1f","repo":"paascloud/paascloud-master","slug":"omc10031014","errorCode":"OMC10031014","errorMessage":"OMC10031014","messagePattern":"OMC10031014","errorType":"error_code","errorClass":"OmcBizException","httpStatus":null,"severity":"error","filePath":"paascloud-provider/paascloud-provider-omc/src/main/java/com/paascloud/provider/service/impl/OmcCartServiceImpl.java","lineNumber":187,"sourceCode":"\t\tLong userId = authDto.getUserId();\n\t\tPreconditions.checkArgument(productId != null, \"货品ID不能为空\");\n\t\tPreconditions.checkArgument(userId != null, ErrorCodeEnum.UAC10011001.msg());\n\n\t\tomcCart.setUpdateInfo(authDto);\n\t\tOmcCart omcCartExist = omcCartMapper.selectByProductIdAndUserId(productId, userId);\n\t\tif (PublicUtil.isEmpty(omcCartExist)) {\n\t\t\ttry {\n\t\t\t\tomcCartMapper.insertSelective(omcCart);\n\t\t\t} catch (Exception e) {\n\t\t\t\tlogger.error(\"新增购物车, 出现异常={}\", e.getMessage(), e);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tomcCart.setId(omcCartExist.getId());\n\t\tomcCart.setQuantity(omcCart.getQuantity() + omcCartExist.getQuantity());\n\t\tint updateResult = omcCartMapper.updateByPrimaryKeySelective(omcCart);\n\t\tif (updateResult < 1) {\n\t\t\tthrow new OmcBizException(ErrorCodeEnum.OMC10031014, omcCartExist.getId());\n\t\t}\n\n\t}\n\n\t@Override\n\tpublic int saveCart(Long userId, Long productId, int count) {\n\t\tlogger.info(\"saveCart - 保存购物车记录 userId={}, productId={}, count={}\", userId, productId, count);\n\n\t\tPreconditions.checkArgument(userId != null, ErrorCodeEnum.UAC10011001.msg());\n\t\tPreconditions.checkArgument(productId != null, ErrorCodeEnum.MDC10021021.msg());\n\t\tPreconditions.checkArgument(count != 0, \"数量不符\");\n\n\t\tint resultInt = 0;\n\t\tOmcCart cart = this.getCartByUserIdAndProductId(userId, productId);\n\t\tif (cart == null) {\n\t\t\tcart = new OmcCart();\n\t\t\tcart.setQuantity(count);\n\t\t\tcart.setChecked(OmcApiConstant.Cart.CHECKED);","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/paascloud/paascloud-master/blob/781281a9503332ed3cef44ea618349d14230a127/paascloud-provider/paascloud-provider-omc/src/main/java/com/paascloud/provider/service/impl/OmcCartServiceImpl.java#L169-L205","documentation":"OMC10031014 maps to ErrorCodeEnum OMC10031014 (\"更新购物车数据失败, cartId=%s\" — failed to update cart data for the given cartId). In OmcCartServiceImpl.saveCart's update path, when the cart item already exists the code merges quantities and calls omcCartMapper.updateByPrimaryKeySelective; if the update affects fewer than 1 row, an OmcBizException is thrown with the existing cart's id. It indicates the UPDATE statement did not persist — the row vanished mid-transaction, a concurrent delete won the race, or a DB error occurred.","triggerScenarios":"Adding to an existing cart item (omcCartExist != null) but omcCartMapper.updateByPrimaryKeySelective returns 0 — e.g. the row was deleted by a concurrent request between the select and the update, the id is stale, or the database rejected/rolled back the update.","commonSituations":"Two tabs/devices updating the same cart simultaneously and one deletes the item; a background job cleared the cart during checkout; DB connection or constraint failure silently reducing the affected-row count; replica/primary lag with stale omcCartExist data.","solutions":["Retry the operation once: re-read the cart row (selectByPrimaryKey on omcCartExist.getId()); if it no longer exists, insert a new row instead of failing.","Serialize cart updates per user (distributed lock or SELECT ... FOR UPDATE on the omc_cart row) to avoid concurrent update/delete races.","Enable SQL logging and check the mapper XML/update statement for conditions (e.g. optimistic-lock version or wrong key) that could match 0 rows.","Check the database error log for a swallowed exception or rollback around the update.","If the delete was intentional, treat the missing row as success/idempotent rather than throwing."],"exampleFix":"// before\nint updateResult = omcCartMapper.updateByPrimaryKeySelective(omcCart);\nif (updateResult < 1) {\n    throw new OmcBizException(ErrorCodeEnum.OMC10031014, omcCartExist.getId());\n}\n// after\nint updateResult = omcCartMapper.updateByPrimaryKeySelective(omcCart);\nif (updateResult < 1) {\n    logger.warn(\"购物车更新未命中, 可能已被并发删除, 重新插入, cartId={}\", omcCartExist.getId());\n    omcCart.setId(null);\n    omcCartMapper.insertSelective(omcCart);\n}","handlingStrategy":"retry","validationCode":"OmcCart existing = omcCartMapper.selectByPrimaryKey(omcCartExist.getId());\nif (existing == null) {\n    // row already deleted concurrently — insert a fresh cart row instead of updating\n    omcCartMapper.insertSelective(omcCart);\n    return;\n}","typeGuard":"boolean canUpdate(OmcCart existing) { return existing != null && existing.getId() != null; }","tryCatchPattern":"try {\n    cartService.updateCartList(loginUser, list);\n} catch (OmcBizException e) {\n    if (String.valueOf(e.getMessage()).contains(\"更新购物车数据失败\")) {\n        // re-read the cart row and retry once with fresh state\n    } else { throw e; }\n}","preventionTips":["Lock cart rows per user during updates (SELECT ... FOR UPDATE or a distributed lock).","Check affected-row counts and reconcile with a re-read before throwing.","Enable SQL logging to catch 0-row updates caused by mismatched WHERE conditions.","Make cart add/update idempotent so concurrent requests converge instead of failing."],"tags":["cart","database-update","concurrency","mybatis","java"],"backgroundTag":"database-write-failed","analyzedSha":"781281a9503332ed3cef44ea618349d14230a127","analyzedAt":"2026-09-10T10:59:02.070Z","contentChangedAt":"2026-09-10T10:59:02.070Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}