{"record":{"id":"6dd82cd900a7aa07","repo":"NationalSecurityAgency/ghidra","slug":"database-in-use","errorCode":null,"errorMessage":"database in use","messagePattern":"database in use","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/client/PostgresFunctionDatabase.java","lineNumber":247,"sourceCode":"\t\t\t\t// database integrity should still be recoverable)\n\t\t\t\tif (asynchronous) {\n\t\t\t\t\tst.executeUpdate(\"SET SESSION synchronous_commit TO OFF\");\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tst.executeUpdate(\"SET SESSION synchronous_commit to ON\");\n\t\t\t\t}\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());\n\t\t}\n\t}\n\n\t@Override\n\tprotected void dropDatabase() throws SQLException {\n\n\t\tif (getStatus() == Status.Busy || postgresDs.getActiveConnections() != 0) {\n\t\t\tthrow new SQLException(\"database in use\");\n\t\t}\n\n\t\tBSimServerInfo serverInfo = postgresDs.getServerInfo();\n\t\tBSimServerInfo defaultServerInfo =\n\t\t\tnew BSimServerInfo(DBType.postgres, serverInfo.getUserInfo(),\n\t\t\t\tserverInfo.getServerName(), serverInfo.getPort(), DEFAULT_DATABASE_NAME);\n\n\t\tBSimPostgresDataSource defaultDs =\n\t\t\tBSimPostgresDBConnectionManager.getDataSource(defaultServerInfo);\n\t\tif (getStatus() == Status.Ready) {\n\t\t\tdefaultDs.initializeFrom(postgresDs);\n\t\t}\n\n\t\tclose(); // close this instance\n\n\t\ttry (Connection defaultDb = defaultDs.getConnection();\n\t\t\t\tStatement defaultSt = defaultDb.createStatement()) {\n\t\t\tStringBuilder sb = new StringBuilder(\"SELECT 1 FROM pg_database WHERE datname= \");","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/client/PostgresFunctionDatabase.java#L229-L265","documentation":"Thrown by PostgresFunctionDatabase.dropDatabase() as a precondition check before dropping. If the database status is Busy (an operation is in progress) or there are active connections (postgresDs.getActiveConnections() != 0), the drop is refused to avoid corrupting in-flight work.","triggerScenarios":"Calling dropDatabase() while another thread or process holds an open connection to the database, or while the database's own status is Status.Busy. The guard fires before any DROP DATABASE SQL is issued.","commonSituations":"A concurrent query or ingest is running against the database. A connection pool still holds open connections that have not been released. The application did not call close() on all database handles before attempting to drop. A previous operation left the status in Busy.","solutions":["Call close() on all FunctionDatabase / connection handles and ensure no concurrent operations are running before dropping.","Wait for in-flight queries to complete or cancel them.","Verify postgresDs.getActiveConnections() returns 0 and getStatus() is not Busy before calling dropDatabase()."],"exampleFix":"// before\ndb.dropDatabase(); // throws \"database in use\" if connections open\n\n// after\ndb.close();\n// ensure no other thread holds a connection\nif (db.getStatus() != Status.Busy && db.getActiveConnections() == 0) {\n    db.dropDatabase();\n}","handlingStrategy":"validation","validationCode":"// Ensure no active connections before dropping\ndb.close();\nif (db.getStatus() == Status.Busy) {\n    throw new IllegalStateException(\"Database is busy; cannot drop now\");\n}\n// verify externally that no other process holds a connection","typeGuard":null,"tryCatchPattern":"try {\n    db.dropDatabase();\n} catch (SQLException e) {\n    if (e.getMessage().equals(\"database in use\")) {\n        // close all connections, wait for operations to finish, then retry\n        db.close();\n        // retry after ensuring zero active connections\n    } else throw e;\n}","preventionTips":["Call close() on all database handles before dropDatabase().","Ensure no concurrent threads or processes are connected.","Check getStatus() and active connection count before dropping."],"tags":["bsim","ghidra","postgresql","database","concurrency","state"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}