testcontainers/testcontainers-java · error · ScriptStatementFailedException

Script statement failed at line lineNumber in script…

Error message

Script statement failed at line lineNumber in script scriptPath (statement: statement)

What it means

CassandraDatabaseDelegate.execute runs a CQL statement and checks ResultSet.wasApplied(). If the statement did not apply (e.g. a conditional LWT or DDL that was rejected/rolled back), it throws ScriptStatementFailedException with statement, line number, and script path. This is the 'not applied' branch, without a driver-level exception.

Solutions

  1. Make statements idempotent or guard with IF NOT EXISTS and tolerate the not-applied result
  2. Inspect the statement at the reported script line and run it manually via cqlsh to see why it was not applied
  3. If state from a previous run conflicts, use a fresh container or drop the keyspace first

Example fix

// before (init.cql)
CREATE TABLE test.users (id uuid PRIMARY KEY, name text);
// after (safe on re-run)
CREATE TABLE IF NOT EXISTS test.users (id uuid PRIMARY KEY, name text);
Defensive patterns

Strategy: validation

Validate before calling

// Prefer statements that report applied=true; guard DDL with IF NOT EXISTS

Try / catch

try { container.start(); } catch (ContainerLaunchException e) { /* ScriptStatementFailedException names statement, line, script */ }

Prevention

When it happens

Trigger: execute(statement, ...) where getConnection().execute(statement) returns a ResultSet with wasApplied()==false — typically a failed conditional (IF EXISTS / IF NOT EXISTS) statement or a rejected statement surfaced as not-applied.

Common situations: Init scripts using CREATE TABLE IF NOT EXISTS where the object already exists; lightweight transactions whose condition failed; re-running scripts against a reused container.

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12). Data as JSON: /api/errors/4931a30d66a7680f. Report an issue: GitHub.

Appendix: source

Thrown at modules/cassandra/src/main/java/org/testcontainers/containers/delegate/CassandraDatabaseDelegate.java:49

            log.error("Could not obtain cassandra connection");
            throw new ConnectionCreationException("Could not obtain cassandra connection", e);
        }
    }

    @Override
    public void execute(
        String statement,
        String scriptPath,
        int lineNumber,
        boolean continueOnError,
        boolean ignoreFailedDrops
    ) {
        try {
            ResultSet result = getConnection().execute(statement);
            if (result.wasApplied()) {
                log.debug("Statement {} was applied", statement);
            } else {
                throw new ScriptStatementFailedException(statement, lineNumber, scriptPath);
            }
        } catch (DriverException e) {
            throw new ScriptStatementFailedException(statement, lineNumber, scriptPath, e);
        }
    }

    @Override
    protected void closeConnectionQuietly(Session session) {
        try {
            session.getCluster().close();
        } catch (Exception e) {
            log.error("Could not close cassandra connection", e);
        }
    }
}

View on GitHub (pinned to 8e549514e3)