apache/cassandra · info · ClientWarn

`USE ` with prepared statements is considered to be an…

Error message

`USE <keyspace>` with prepared statements is considered to be an anti-pattern due to ambiguity in non-qualified table names. Please consider removing instances of `Session#setKeyspace(<keyspace>)`, `Session#execute("USE <keyspace>")` and `cluster.newSession(<keyspace>)` and always use fully qualified table names (e.g. <keyspace>.<table>). Keyspace used: %s, statement keyspace: %s, statement id: %s

What it means

A client warning (sent to the driver via ClientWarn, not a server log) issued when a prepared statement's keyspace is changed with USE or Session#setKeyspace. Mixing USE with prepared statements creates ambiguity for non-qualified table names and is flagged as an anti-pattern; the warning is issued once per ClientState session.

Solutions

  1. Remove USE/setKeyspace calls and fully qualify table names (keyspace.table) in all statements
  2. Create a session bound to the keyspace instead (cluster.connect(keyspace)) without issuing USE between prepares
  3. Re-prepare statements after any intentional keyspace switch; treat the warning as a code smell to fix, not suppress

Example fix

// before
session.execute("USE myks");
PreparedStatement ps = session.prepare("SELECT * FROM mytable");
// after
PreparedStatement ps = session.prepare("SELECT * FROM myks.mytable");
Defensive patterns

Strategy: validation

Validate before calling

// assert no USE statements are issued alongside prepared statements
String cql = statement.trim().toLowerCase();
if (cql.startsWith("use ") && session.getPreparedStatements().count() > 0)
    throw new IllegalArgumentException("Do not USE keyspace while using prepared statements; qualify table names");

Try / catch

// driver-side: consume warnings so the anti-pattern surfaces
cluster.register(warnings -> warnings.forEach(w -> log.warn("Server warning: {}", w)));

Prevention

When it happens

Trigger: Client executes `USE <keyspace>` (or Session#setKeyspace / cluster.newSession semantics) after prepare() has recorded prepared statements on this ClientState; the next prepare or execution path calls warnAboutUseWithPreparedStatements.

Common situations: Applications sharing one session/connection pool that switch keyspace with USE before binding prepared statements; drivers that auto-issue USE; scripts ported from cqlsh habits into driver code.

Understand the failure class

Background: "is deprecated and will be removed" — deprecation warnings for old API names, keywords, and options, and how to migrate before the removal release — this error's family across 29 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/867f3f052abce7d8. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/service/ClientState.java:716

     *
     * @return {@code true} if this user is the system user, {@code false} otherwise.
     */
    public boolean isSystem()
    {
        return isInternal;
    }

    public void ensureIsSuperuser(String message)
    {
        if (!isSuper())
            throw new UnauthorizedException(message);
    }

    public void warnAboutUseWithPreparedStatements(MD5Digest statementId, String preparedKeyspace)
    {
        if (!issuedPreparedStatementsUseWarning)
        {
            ClientWarn.instance.warn(String.format("`USE <keyspace>` with prepared statements is considered to be an anti-pattern due to ambiguity in non-qualified table names. " +
                                                   "Please consider removing instances of `Session#setKeyspace(<keyspace>)`, `Session#execute(\"USE <keyspace>\")` and `cluster.newSession(<keyspace>)` from your code, and " +
                                                   "always use fully qualified table names (e.g. <keyspace>.<table>). " +
                                                   "Keyspace used: %s, statement keyspace: %s, statement id: %s", getRawKeyspace(), preparedKeyspace, statementId));
            issuedPreparedStatementsUseWarning = true;
        }
    }

    public void warnAboutUneligiblePreparedStatement(MD5Digest statementId)
    {
        if (!issuedWarningForUneligiblePreparedStatements)
        {
            ClientWarn.instance.warn(String.format("Prepared statements for other than modification and selection statements should be avoided, statement id: %s", statementId));
            issuedWarningForUneligiblePreparedStatements = true;
        }
    }

    private static void validateKeyspace(String keyspace)
    {

View on GitHub (pinned to 88fd0f6a0e)