{"record":{"id":"f90c8f1d7c72ecc3","repo":"apolloconfig/apollo","slug":"appnamespace-not-unique","errorCode":null,"errorMessage":"appnamespace not unique","messagePattern":"appnamespace not unique","errorType":"exception","errorClass":"ServiceException","httpStatus":500,"severity":"error","filePath":"apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/AppNamespaceService.java","lineNumber":108,"sourceCode":"  public AppNamespace findOne(String appId, String namespaceName) {\n    Preconditions.checkArgument(!StringUtils.isContainEmpty(appId, namespaceName),\n        \"appId or Namespace must not be null\");\n    return appNamespaceRepository.findByAppIdAndName(appId, namespaceName);\n  }\n\n  public List<AppNamespace> findByAppIdAndNamespaces(String appId, Set<String> namespaceNames) {\n    Preconditions.checkArgument(!Strings.isNullOrEmpty(appId), \"appId must not be null\");\n    if (namespaceNames == null || namespaceNames.isEmpty()) {\n      return Collections.emptyList();\n    }\n    return appNamespaceRepository.findByAppIdAndNameIn(appId, namespaceNames);\n  }\n\n  @Transactional\n  @ApolloAuditLog(type = OpType.CREATE, name = \"AppNamespace.createDefault\")\n  public void createDefaultAppNamespace(String appId, String createBy) {\n    if (!isAppNamespaceNameUnique(appId, ConfigConsts.NAMESPACE_APPLICATION)) {\n      throw new ServiceException(\"appnamespace not unique\");\n    }\n    AppNamespace appNs = new AppNamespace();\n    appNs.setAppId(appId);\n    appNs.setName(ConfigConsts.NAMESPACE_APPLICATION);\n    appNs.setComment(\"default app namespace\");\n    appNs.setFormat(ConfigFileFormat.Properties.getValue());\n    appNs.setDataChangeCreatedBy(createBy);\n    appNs.setDataChangeLastModifiedBy(createBy);\n    appNamespaceRepository.save(appNs);\n\n    auditService.audit(AppNamespace.class.getSimpleName(), appNs.getId(), Audit.OP.INSERT,\n        createBy);\n  }\n\n  @Transactional\n  public AppNamespace createAppNamespace(AppNamespace appNamespace) {\n    String createBy = appNamespace.getDataChangeCreatedBy();\n    if (!isAppNamespaceNameUnique(appNamespace.getAppId(), appNamespace.getName())) {","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/apolloconfig/apollo/blob/d95fc18d112589efc09ddcbe1507047584d55251/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/AppNamespaceService.java#L90-L126","documentation":"A ServiceException (HTTP 500) thrown from AppNamespaceService.createDefaultAppNamespace() when the default 'application' namespace (ConfigConsts.NAMESPACE_APPLICATION) already exists for the given appId. The uniqueness check isAppNamespaceNameUnique() queries appNamespaceRepository.findByAppIdAndName(). This is called during initial app creation to bootstrap the mandatory default namespace; a duplicate means the app was already partially or fully initialized.","triggerScenarios":"createDefaultAppNamespace(appId, createBy) is called (typically during App creation) when an AppNamespace row already exists for appId + name='application'. This can happen if the app-creation flow is retried or if the namespace was created out-of-band.","commonSituations":"App creation request retried after a partial failure (app row committed but the error was returned to client); a database restore that left the namespace in place while the client re-ran creation; race condition between two concurrent app-creation requests; manual database intervention that pre-created the namespace.","solutions":["Check whether the app and its default namespace already exist before calling createDefaultAppNamespace (idempotency guard).","If the app already exists with its default namespace, treat the error as benign and skip re-creation.","Use the isAppNamespaceNameUnique() check before invoking creation.","Investigate partial-failure cleanup if the app creation transaction is not atomic."],"exampleFix":"// before: unconditional creation\nappService.save(app);\nappNamespaceService.createDefaultAppNamespace(app.getAppId(), operator);\n\n// after: check before creating\nif (appNamespaceService.isAppNamespaceNameUnique(app.getAppId(), \"application\")) {\n    appNamespaceService.createDefaultAppNamespace(app.getAppId(), operator);\n}","handlingStrategy":"validation","validationCode":"// Check before creating the default namespace\nif (appNamespaceService.isAppNamespaceNameUnique(appId, ConfigConsts.NAMESPACE_APPLICATION)) {\n    appNamespaceService.createDefaultAppNamespace(appId, createBy);\n} else {\n    // default namespace already exists — app may be partially initialized\n    logger.info(\"Default namespace already exists for app {}\", appId);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make app-creation flows idempotent by checking for existing default namespace first.","Avoid retrying app-creation requests without verifying partial completion.","Use isAppNamespaceNameUnique() as a pre-check before any default-namespace creation."],"tags":["namespace","duplicate","default-namespace","service-exception","apollo-biz"],"backgroundTag":null,"analyzedSha":"d95fc18d112589efc09ddcbe1507047584d55251","analyzedAt":"2026-08-14T04:00:05.477Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}