t8y2/dbx · error · java.lang.IllegalStateException

Not connected

Error message

Not connected

What it means

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.

Source

Thrown at agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java:1885

                    System.exit(0);
                }
                yield Collections.singletonMap("ok", true);
            }
            default -> throw new IllegalArgumentException("Unknown method: " + method);
        };
    }

    static AgentProtocol.HandshakeResult runtimeHandshakeResult() {
        return AgentProtocol.mongoLegacyMultiSessionHandshakeResult();
    }

    private static MongoClient requireClient() {
        MongoClient client = CURRENT_CLIENT.get();
        if (client == null) {
            client = legacyClient;
        }
        if (client == null) {
            throw new IllegalStateException("Not connected");
        }
        return client;
    }

    private static void closeLegacyClient() {
        if (legacyClient != null) {
            legacyClient.close();
            legacyClient = null;
        }
    }

    private static String coalesce(String value) {
        return value == null ? "" : value;
    }

    private static String defaultString(String value, String fallback) {
        return value == null ? fallback : value;
    }

View on GitHub (pinned to c0390bff16)

Solutions

  1. Call the agent's connect (or open a session with openClient) before issuing data operations.
  2. Reconnect after any shutdown/agent restart; treat Not connected as 'must re-handshake'.
  3. In multi-session mode, route each request to a valid open sessionId so CURRENT_CLIENT is populated.
  4. Check connect response/logs for earlier failures that left the client unset.

Example fix

// before
{"method": "find", "params": {"database": "app", "collection": "users"}}
// after
{"method": "connect", "params": {"uri": "mongodb://localhost:27017"}}
{"method": "find", "params": {"database": "app", "collection": "users"}}
Defensive patterns

Strategy: try-catch

Validate before calling

// Java: ensure connect succeeded before data calls
if (!agentConnection.isOpen()) {
    agentConnection.connect(uri); // establish client first
}

Try / catch

try { result = agent.dispatch(op); }
catch (IllegalStateException e) {
  if ("Not connected".equals(e.getMessage())) {
    agent.connect(uri);           // re-handshake
    result = agent.dispatch(op);  // retry once
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of t8y2/dbx@c0390bff16 (2026-09-05). Data as JSON: /api/errors/ad5c9f1d587a11a0. Report an issue: GitHub.