{"record":{"id":"ed383d2140811898","repo":"NationalSecurityAgency/ghidra","slug":"database-already-exists","errorCode":null,"errorMessage":"Database already exists: {}","messagePattern":"Database already exists: (.+?)","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/file/H2FileFunctionDatabase.java","lineNumber":102,"sourceCode":"\tprotected void setConnectionOnTables(Connection db) {\n\t\tvectorTable.setConnection(db);\n\t\tsuper.setConnectionOnTables(db);\n\t}\n\n\t@Override\n\tprotected Connection initConnection() throws SQLException {\n\t\tif (getStatus() != Status.Ready && !fileDs.exists()) {\n\t\t\tthrow new SQLException(\n\t\t\t\t\"Database does not exist: \" + fileDs.getServerInfo().getDBName());\n\t\t}\n\t\treturn super.initConnection();\n\t}\n\n\t@Override\n\tprotected void generateRawDatabase() throws SQLException {\n\t\tBSimServerInfo serverInfo = fileDs.getServerInfo();\n\t\tif (fileDs.exists()) {\n\t\t\tthrow new SQLException(\"Database already exists: \" + serverInfo.getDBName());\n\t\t}\n\t\ttry (Connection c = fileDs.getConnection()) {\n\t\t\t// do nothing - should throw exception on error\n\t\t}\n\t}\n\n\t@Override\n\tprotected void createDatabase(Configuration config) throws SQLException {\n\t\ttry {\n\t\t\tsuper.createDatabase(config);\n\n\t\t\tConnection db = initConnection();\n\t\t\ttry (Statement st = db.createStatement()) {\n\t\t\t\tvectorTable.create(st);\n\t\t\t}\n\t\t}\n\t\tcatch (final SQLException err) {\n\t\t\tthrow new SQLException(\"Could not create database: \" + err.getMessage());","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/file/H2FileFunctionDatabase.java#L84-L120","documentation":"Thrown as an SQLException by H2FileFunctionDatabase.generateRawDatabase() when the H2 file data source already exists on disk. This method is called during database creation and expects to start from scratch, so an existing file is treated as an error to prevent silent data loss or corruption.","triggerScenarios":"Occurs when createDatabase/generateRawDatabase is called for an H2 URL whose target file already exists. This happens when: running a 'create' command on a database name that's already been created, re-running a BSim database generation script without first dropping the old database, or pointing two create operations at the same file.","commonSituations":"Running a BSim 'generate' headless command on a file that was previously generated. Scripting a database creation pipeline without checking for existing output. Accidentally reusing a database name from a prior run.","solutions":["If the existing database is stale or unwanted, drop it first using dropDatabase() or delete the file manually.","If the existing database is valid, connect to it instead of creating a new one (use the read/connect path rather than generateRawDatabase).","Use a different database name/path for the new database.","Add a pre-creation check: if fileDs.exists(), skip or prompt the user."],"exampleFix":"// before\ndb.generateRawDatabase(); // throws if file exists\n\n// after\nif (fileDs.exists()) {\n    if (overwrite) {\n        db.dropDatabase();\n    } else {\n        throw new IllegalStateException(\"Database exists, use overwrite=true or different name\");\n    }\n}\ndb.generateRawDatabase();","handlingStrategy":"validation","validationCode":"// Check if database exists before attempting creation\nif (fileDs.exists()) {\n    if (overwriteAllowed) {\n        database.dropDatabase();\n    } else {\n        throw new IllegalStateException(\n            \"Database already exists: \" + serverInfo.getDBName() +\n            \". Use overwrite=true to replace.\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    database.generateRawDatabase();\n} catch (SQLException e) {\n    if (e.getMessage().startsWith(\"Database already exists:\")) {\n        // Decide: drop and recreate, or use existing\n        if (overwrite) {\n            database.dropDatabase();\n            database.generateRawDatabase();\n        }\n    } else {\n        throw e;\n    }\n}","preventionTips":["Check fileDs.exists() before calling generateRawDatabase().","Add a --force/--overwrite flag to BSim generation scripts.","Use unique database names for each generation run to avoid collisions.","Document the create-vs-connect distinction clearly for users."],"tags":["bsim","h2","database","already-exists","sql"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}