{"record":{"id":"ad5c9f1d587a11a0","repo":"t8y2/dbx","slug":"not-connected-ad5c9f","errorCode":null,"errorMessage":"Not connected","messagePattern":"Not connected","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":1885,"sourceCode":"                    System.exit(0);\n                }\n                yield Collections.singletonMap(\"ok\", true);\n            }\n            default -> throw new IllegalArgumentException(\"Unknown method: \" + method);\n        };\n    }\n\n    static AgentProtocol.HandshakeResult runtimeHandshakeResult() {\n        return AgentProtocol.mongoLegacyMultiSessionHandshakeResult();\n    }\n\n    private static MongoClient requireClient() {\n        MongoClient client = CURRENT_CLIENT.get();\n        if (client == null) {\n            client = legacyClient;\n        }\n        if (client == null) {\n            throw new IllegalStateException(\"Not connected\");\n        }\n        return client;\n    }\n\n    private static void closeLegacyClient() {\n        if (legacyClient != null) {\n            legacyClient.close();\n            legacyClient = null;\n        }\n    }\n\n    private static String coalesce(String value) {\n        return value == null ? \"\" : value;\n    }\n\n    private static String defaultString(String value, String fallback) {\n        return value == null ? fallback : value;\n    }","sourceCodeStart":1867,"sourceCodeEnd":1903,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L1867-L1903","documentation":"requireClient() resolves the current MongoClient from the per-request thread-local CURRENT_CLIENT, falling back to the legacy shared client. If both are absent, no connection has been established for this request, so it throws IllegalStateException(\"Not connected\") instead of dereferencing a null client.","triggerScenarios":"Sending any data-plane method (find/insert/update/delete) to the agent before a connect/open-session call succeeded; the legacy client was closed by a shutdown/close; using multi-session mode where the method ran on a thread whose CURRENT_CLIENT was never set.","commonSituations":"Agent restart wiping the legacy connection while clients keep sending requests; forgetting the initial `connect` handshake; session closed by another caller while work is in flight; connection failure during startup silently leaving the client null.","solutions":["Call the agent's connect (or open a session with openClient) before issuing data operations.","Reconnect after any shutdown/agent restart; treat Not connected as 'must re-handshake'.","In multi-session mode, route each request to a valid open sessionId so CURRENT_CLIENT is populated.","Check connect response/logs for earlier failures that left the client unset."],"exampleFix":"// before\n{\"method\": \"find\", \"params\": {\"database\": \"app\", \"collection\": \"users\"}}\n// after\n{\"method\": \"connect\", \"params\": {\"uri\": \"mongodb://localhost:27017\"}}\n{\"method\": \"find\", \"params\": {\"database\": \"app\", \"collection\": \"users\"}}","handlingStrategy":"try-catch","validationCode":"// Java: ensure connect succeeded before data calls\nif (!agentConnection.isOpen()) {\n    agentConnection.connect(uri); // establish client first\n}","typeGuard":null,"tryCatchPattern":"try { result = agent.dispatch(op); }\ncatch (IllegalStateException e) {\n  if (\"Not connected\".equals(e.getMessage())) {\n    agent.connect(uri);           // re-handshake\n    result = agent.dispatch(op);  // retry once\n  } else throw e;\n}","preventionTips":["Establish the connection during agent startup and verify the connect response","Treat agent restarts as requiring full re-handshake; listen for connection-loss events","In multi-session mode, bind each request to an open sessionId"],"tags":["mongodb","connection","state","lifecycle"],"backgroundTag":"not-connected","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}