{"record":{"id":"ed2aad8dce201fba","repo":"apache/iceberg","slug":"databasealreadyexistexception","errorCode":null,"errorMessage":"DatabaseAlreadyExistException","messagePattern":"DatabaseAlreadyExistException","errorType":"exception","errorClass":"DatabaseAlreadyExistException","httpStatus":null,"severity":"error","filePath":"flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/FlinkCatalog.java","lineNumber":228,"sourceCode":"    }\n  }\n\n  @Override\n  public void createDatabase(String name, CatalogDatabase database, boolean ignoreIfExists)\n      throws DatabaseAlreadyExistException, CatalogException {\n    createDatabase(\n        name, mergeComment(database.getProperties(), database.getComment()), ignoreIfExists);\n  }\n\n  private void createDatabase(\n      String databaseName, Map<String, String> metadata, boolean ignoreIfExists)\n      throws DatabaseAlreadyExistException, CatalogException {\n    if (asNamespaceCatalog != null) {\n      try {\n        asNamespaceCatalog.createNamespace(appendLevel(baseNamespace, databaseName), metadata);\n      } catch (AlreadyExistsException e) {\n        if (!ignoreIfExists) {\n          throw new DatabaseAlreadyExistException(getName(), databaseName, e);\n        }\n      }\n    } else {\n      throw new UnsupportedOperationException(\n          \"Namespaces are not supported by catalog: \" + getName());\n    }\n  }\n\n  private Map<String, String> mergeComment(Map<String, String> metadata, String comment) {\n    Map<String, String> ret = Maps.newHashMap(metadata);\n    if (metadata.containsKey(\"comment\")) {\n      throw new CatalogException(\"Database properties should not contain key: 'comment'.\");\n    }\n\n    if (!StringUtils.isNullOrWhitespaceOnly(comment)) {\n      ret.put(\"comment\", comment);\n    }\n    return ret;","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/FlinkCatalog.java#L210-L246","documentation":"FlinkCatalog.createDatabase delegates to the namespace catalog's createNamespace; if Iceberg reports AlreadyExistsException and ignoreIfExists is false, it rethrows as Flink's DatabaseAlreadyExistException. The database with that name (under the configured base namespace) is already present, and the caller asked for strict creation semantics.","triggerScenarios":"CREATE DATABASE db (without IF NOT EXISTS) or createDatabase(name, meta, false) where the namespace already exists in the backing store — often from re-running idempotent setup scripts or concurrent jobs creating the same database.","commonSituations":"Replayed SQL migration scripts against an initialized warehouse; two Flink jobs bootstrapping the same database concurrently; CI environments reusing persistent catalog state.","solutions":["Use CREATE DATABASE IF NOT EXISTS in SQL or pass ignoreIfExists=true in createDatabase.","Check existence first with databaseExists(name) before creating.","If the existing database is stale, drop it explicitly before recreating.","Serialize bootstrap steps (job scheduling/locks) if concurrent creation is the cause."],"exampleFix":"-- before\nCREATE DATABASE analytics;\n-- after\nCREATE DATABASE IF NOT EXISTS analytics;","handlingStrategy":"validation","validationCode":"// idempotent creation guard\nif (!catalog.databaseExists(name)) {\n  catalog.createDatabase(name, metadata);\n}","typeGuard":null,"tryCatchPattern":"try {\n  catalog.createDatabase(name, metadata, false);\n} catch (DatabaseAlreadyExistException e) {\n  log.info(\"database {} already exists; continuing\", name); // idempotent bootstrap\n}","preventionTips":["Prefer CREATE DATABASE IF NOT EXISTS / ignoreIfExists=true in setup scripts","Serialize bootstrap steps when multiple jobs may create the same database","Use persistent-state-aware CI so setup does not replay against existing warehouses"],"tags":["flink","catalog","namespace"],"backgroundTag":"file-already-exists","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}