{"record":{"id":"f8248e05f319f2a8","repo":"NationalSecurityAgency/ghidra","slug":"attempted-to-drop-non-bsim-database-f8248e","errorCode":null,"errorMessage":"attempted to drop non-BSim database","messagePattern":"attempted to drop non-BSim database","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/file/H2FileFunctionDatabase.java","lineNumber":152,"sourceCode":"\t\t\t// ignore request and return\n\t\t\treturn;\n\t\t}\n\n\t\t// Connect to database and examine schema\n\t\tHashSet<String> tableNames = new HashSet<>();\n\t\ttry (Connection c = initConnection(); Statement st = c.createStatement()) {\n\t\t\ttry (ResultSet rs = st.executeQuery(\n\t\t\t\t\"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name\")) {\n\t\t\t\twhile (rs.next()) {\n\t\t\t\t\ttableNames.add(rs.getString(1));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Spot check for a few BSim table names that always exist\n\t\tif (!tableNames.contains(\"keyvaluetable\") || !tableNames.contains(\"desctable\") ||\n\t\t\t!tableNames.contains(\"weighttable\")) {\n\t\t\tthrow new SQLException(\"attempted to drop non-BSim database\");\n\t\t}\n\n\t\tfileDs.dispose(); // disconnect before deleting database\n\n\t\tBSimServerInfo serverInfo = fileDs.getServerInfo();\n\t\tif (!fileDs.delete()) {\n\t\t\tthrow new SQLException(\"failed to delete H2-file database: \" + serverInfo);\n\t\t}\n\n\t\tMsg.info(this, \"Deleted BSim H2-file database: \" + serverInfo);\n\n\t}\n\n\t/**\n\t * Create vector map which maps vector ID to {@link VectorStoreEntry}\n\t * @return vector map\n\t * @throws SQLException if error occurs while reading map data\n\t */","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/file/H2FileFunctionDatabase.java#L134-L170","documentation":"Thrown as an SQLException by H2FileFunctionDatabase.dropDatabase() when the database file exists and is connectable, but its schema does not contain the expected BSim tables (keyvaluetable, desctable, weighttable). This is a safety guard that prevents dropping a non-BSim H2 file that happens to live at the same path.","triggerScenarios":"Occurs when the information_schema query returns table names but the three mandatory BSim tables are absent. This happens when: the H2 file at the path is a different application's database, a partially-created BSim database that failed before all tables were written, or a file that was manually modified or replaced.","commonSituations":"Pointing a BSim drop command at an arbitrary H2 file. A previous createDatabase call failed partway through, leaving an incomplete schema. The file path collides with another tool's H2 database.","solutions":["Verify the target file is actually a BSim H2 database by checking for the expected tables.","If the database is a partial/failed creation, manually delete the file rather than using dropDatabase().","Ensure the correct BSim URL is being used for the drop operation.","If the file is genuinely not a BSim database, do not attempt to drop it via BSim APIs."],"exampleFix":"// before\ndb.dropDatabase(); // throws if tables missing\n\n// after\nif (!isBsimDatabase(fileDs)) {\n    // manually clean up non-BSim or partial file\n    Files.deleteIfExists(dbPath);\n} else {\n    db.dropDatabase();\n}","handlingStrategy":"validation","validationCode":"// Verify the H2 file is a BSim database before dropping\nSet<String> bsimTables = Set.of(\"keyvaluetable\", \"desctable\", \"weighttable\");\ntry (Connection c = fileDs.getConnection(); Statement st = c.createStatement();\n        ResultSet rs = st.executeQuery(\n            \"SELECT table_name FROM information_schema.tables WHERE table_schema='public'\")) {\n    Set<String> found = new HashSet<>();\n    while (rs.next()) found.add(rs.getString(1));\n    if (!found.containsAll(bsimTables)) {\n        throw new IllegalStateException(\n            \"Not a BSim database; missing tables: \" + Sets.difference(bsimTables, found));\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    database.dropDatabase();\n} catch (SQLException e) {\n    if (e.getMessage().equals(\"attempted to drop non-BSim database\")) {\n        // This is not a BSim DB; if it's a stale partial, delete manually\n        if (forceDeleteNonBsim) {\n            Files.deleteIfExists(dbPath);\n        } else {\n            showErrorDialog(\"Not a BSim Database\",\n                \"The file is not a valid BSim database and will not be dropped.\");\n        }\n    } else {\n        throw e;\n    }\n}","preventionTips":["Validate the H2 file contains BSim tables before calling dropDatabase().","Use distinct file paths/names for BSim databases to avoid collisions with other H2 files.","If a previous createDatabase failed partially, clean up the file manually rather than via dropDatabase().","Never point BSim operations at arbitrary H2 files."],"tags":["bsim","h2","database","drop","schema-validation","safety-guard"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}