{"record":{"id":"33a5afac124cf587","repo":"apolloconfig/apollo","slug":"namespace-not-unique","errorCode":null,"errorMessage":"namespace not unique","messagePattern":"namespace not unique","errorType":"exception","errorClass":"ServiceException","httpStatus":500,"severity":"error","filePath":"apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/NamespaceService.java","lineNumber":359,"sourceCode":"    namespace.setDataChangeLastModifiedBy(operator);\n\n    auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.DELETE,\n        operator);\n\n    Namespace deleted = namespaceRepository.save(namespace);\n\n    // Publish release message to do some clean up in config service, such as updating the cache\n    messageSender.sendMessage(\n        ReleaseMessageKeyGenerator.generate(appId, clusterName, namespaceName),\n        Topics.APOLLO_RELEASE_TOPIC);\n\n    return deleted;\n  }\n\n  @Transactional\n  public Namespace save(Namespace entity) {\n    if (!isNamespaceUnique(entity.getAppId(), entity.getClusterName(), entity.getNamespaceName())) {\n      throw new ServiceException(\"namespace not unique\");\n    }\n\n    if (bizConfig.isNamespaceNumLimitEnabled()\n        && !bizConfig.namespaceNumLimitWhite().contains(entity.getAppId())) {\n      int nowCount = namespaceRepository.countByAppIdAndClusterName(entity.getAppId(),\n          entity.getClusterName());\n      if (nowCount >= bizConfig.namespaceNumLimit()) {\n        throw new ServiceException(\n            \"namespace[appId = \" + entity.getAppId() + \", cluster= \" + entity.getClusterName()\n                + \"] nowCount= \" + nowCount + \", maxCount =\" + bizConfig.namespaceNumLimit());\n      }\n    }\n\n    entity.setId(0);// protection\n    Namespace namespace = namespaceRepository.save(entity);\n\n    auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.INSERT,\n        namespace.getDataChangeCreatedBy());","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/apolloconfig/apollo/blob/d95fc18d112589efc09ddcbe1507047584d55251/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/NamespaceService.java#L341-L377","documentation":"Thrown as ServiceException (HTTP 500) by NamespaceService.save when isNamespaceUnique(appId, clusterName, namespaceName) is false — the same appId+clusterName+namespaceName combination already exists. Note it surfaces as a 500 ServiceException rather than a 400, which is a known code smell: a duplicate/conflict is reported as a server error.","triggerScenarios":"Creating a namespace via NamespaceService.save when an identical (appId, clusterName, namespaceName) namespace already exists, or two concurrent creates race past the uniqueness check before the DB unique constraint catches one.","commonSituations":"Idempotent re-creation after a retry; scripts that re-run namespace provisioning; a duplicate detected at the service layer rather than the DB.","solutions":["Check existence first with namespaceService.findOne(appId, clusterName, namespaceName) and return the existing one instead of re-saving.","Make provisioning scripts idempotent (skip-if-exists).","Treat this as a 409/400 conflict at the controller layer; consider upstream improvement to throw a conflict-style exception."],"exampleFix":"// before\nnamespaceService.save(newNamespace); // throws 'namespace not unique' if exists\n// after\nNamespace existing = namespaceService.findOne(appId, clusterName, name);\nif (existing != null) {\n  return existing;\n}\nreturn namespaceService.save(newNamespace);","handlingStrategy":"validation","validationCode":"Namespace existing = namespaceService.findOne(entity.getAppId(),\n    entity.getClusterName(), entity.getNamespaceName());\nif (existing != null) {\n  return existing; // idempotent\n}\nreturn namespaceService.save(entity);","typeGuard":null,"tryCatchPattern":"try {\n  return namespaceService.save(entity);\n} catch (ServiceException e) {\n  if (\"namespace not unique\".equals(e.getMessage())) {\n    return namespaceService.findOne(entity.getAppId(), entity.getClusterName(), entity.getNamespaceName());\n  }\n  throw e;\n}","preventionTips":["Make namespace provisioning idempotent with an existence check.","Guard against concurrent creates with the DB unique constraint as the backstop.","Consider mapping this conflict to a 409 at the controller for clearer client semantics."],"tags":["namespace","duplicate","service-error","apollo-biz","conflict"],"backgroundTag":null,"analyzedSha":"d95fc18d112589efc09ddcbe1507047584d55251","analyzedAt":"2026-08-14T04:00:05.477Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}