{"record":{"id":"ac9d8d6b79c2b438","repo":"OtterMind/Chat2DB","slug":"database-delete-connectionfailed","errorCode":"database.delete.connectionFailed","errorMessage":"database.delete.connectionFailed","messagePattern":"database\\.delete\\.connectionFailed","errorType":"validation","errorClass":"BusinessException","httpStatus":null,"severity":"critical","filePath":"chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/db/DbDatabaseObjectDeleteServiceImpl.java","lineNumber":198,"sourceCode":"            throw new BusinessException(\"database.delete.notSupportDatabase\");\n        }\n    }\n\n    private void rejectSystemDatabase(String dbType, String databaseName) {\n        if (DatabaseTypeEnum.MYSQL.name().equalsIgnoreCase(dbType)\n                && MYSQL_SYSTEM_DATABASES.contains(StringUtils.lowerCase(databaseName))) {\n            throw new BusinessException(\"database.delete.systemDatabaseForbidden\");\n        }\n        if (DatabaseTypeEnum.POSTGRESQL.name().equalsIgnoreCase(dbType)\n                && POSTGRESQL_SYSTEM_DATABASES.contains(StringUtils.lowerCase(databaseName))) {\n            throw new BusinessException(\"database.delete.systemDatabaseForbidden\");\n        }\n    }\n\n    private Connection requireConnection() {\n        Connection connection = Chat2DBContext.getConnection();\n        if (connection == null) {\n            throw new BusinessException(\"database.delete.connectionFailed\");\n        }\n        return connection;\n    }\n\n    private void assertDatabaseExists(Connection connection, String dbType, String databaseName) {\n        List<Database> databases = Chat2DBContext.getDbMetaData(dbType).databases(connection);\n        boolean exists = databases != null && databases.stream()\n                .anyMatch(database -> StringUtils.equals(database.getName(), databaseName));\n        if (!exists) {\n            throw new BusinessException(\"database.delete.databaseNotExists\");\n        }\n    }\n\n    private DatabaseObjectDeletePrepare prepareResult(String confirmName, String sqlPreview, String objectType,\n                                                       String dbType) {\n        return DatabaseObjectDeletePrepare.builder()\n                .confirmName(confirmName)\n                .sqlPreview(sqlPreview)","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/OtterMind/Chat2DB/blob/5ee1e990e73fbcae1969dc554be254fedb3ab888/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/db/DbDatabaseObjectDeleteServiceImpl.java#L180-L216","documentation":"Thrown by requireConnection() when Chat2DBContext.getConnection() returns null after the connection context was expected to be established. This means the thread-bound JDBC connection is missing when the delete-prepare or delete-execute operation needs it. It typically indicates the connection was never opened, was already closed, or the context was not bound for this thread/request.","triggerScenarios":"prepareDatabaseDelete or executeDatabaseDelete reaching requireConnection() (lines 68/112) when no connection is bound in Chat2DBContext. For PostgreSQL, reconnectForDatabaseDelete rebinds first; if that rebind fails to establish a connection, getConnection() returns null.","commonSituations":"The datasource connection pool is exhausted or the DB server is unreachable so the context never bound a connection; a code path that calls the service outside the normal request-scoped connection lifecycle; connection was closed by a prior operation or timeout.","solutions":["Verify the datasource can connect: check DB server availability, credentials, and network reachability.","Ensure the service is called within the request scope that binds a connection to Chat2DBContext (via connectionContextService).","Check server logs for prior connection-acquisition failures or pool exhaustion."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (Chat2DBContext.getConnection() == null) {\n    // surface a connection-setup error to the user; do not proceed\n}","typeGuard":null,"tryCatchPattern":"try {\n    service.prepareDatabaseDelete(req);\n} catch (BusinessException e) {\n    if (\"database.delete.connectionFailed\".equals(e.getCode())) {\n        // prompt user to reconnect / check datasource\n    } else { throw e; }\n}","preventionTips":["Ensure the connection-scoping interceptor runs before any delete operation.","Monitor connection-pool health and surface pool-exhaustion early."],"tags":["database-delete","connection","context-missing"],"backgroundTag":null,"analyzedSha":"5ee1e990e73fbcae1969dc554be254fedb3ab888","analyzedAt":"2026-08-14T07:05:03.077Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}