testcontainers/testcontainers-java · error · ScriptUtils.ScriptStatementFailedException

Failed to execute SQL script statement at line

Error message

Failed to execute SQL script statement at line {} of resource {}: {}

What it means

When a statement in an executed SQL script fails and the target is a named resource (scriptPath non-null), JdbcDatabaseDelegate throws ScriptUtils.ScriptStatementFailedException including the line number, resource path and failed statement. It surfaces exactly which SQL line of the script failed against the database.

Solutions

  1. Read the reported line number and statement, then run it manually to see the vendor error
  2. Fix the failing SQL at that line in the script resource
  3. Make the script idempotent (CREATE TABLE IF NOT EXISTS, DROP ... IF EXISTS)
  4. Check the cause exception for the vendor error code and message

Example fix

// before
CREATE TABLE users(id INT PRIMARY KEY);
CREATE TABLE users(id INT PRIMARY KEY); -- duplicate on re-run
// after
CREATE TABLE IF NOT EXISTS users(id INT PRIMARY KEY);
CREATE TABLE IF NOT EXISTS users(id INT PRIMARY KEY);
Defensive patterns

Strategy: try-catch

Try / catch

try {
    delegate.executeScript(scriptResource);
} catch (ScriptUtils.ScriptStatementFailedException e) {
    log.error("Script {} failed at line {}: {}", e.getScript(), e.getLineNumber(), e.getStatement(), e.getCause());
    throw e;
}

Prevention

When it happens

Trigger: Running a script via the delegate (init script path) where one statement fails — syntax error, constraint violation, or object already exists — against the container database.

Common situations: Idempotency issues (re-running CREATE TABLE); dialect mismatches; order-dependent statements; data violating constraints added earlier in the script.

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/2fb19f52d8a39686. Report an issue: GitHub.

Appendix: source

Thrown at modules/jdbc/src/main/java/org/testcontainers/jdbc/JdbcDatabaseDelegate.java:63

        int lineNumber,
        boolean continueOnError,
        boolean ignoreFailedDrops
    ) {
        try {
            boolean rowsAffected = getConnection().execute(statement);
            log.debug("{} returned as updateCount for SQL: {}", rowsAffected, statement);
        } catch (SQLException ex) {
            boolean dropStatement = statement.trim().toLowerCase().startsWith("drop");
            if (continueOnError || (dropStatement && ignoreFailedDrops)) {
                log.debug(
                    "Failed to execute SQL script statement at line {} of resource {}: {}",
                    lineNumber,
                    scriptPath,
                    statement,
                    ex
                );
            } else {
                throw new ScriptUtils.ScriptStatementFailedException(statement, lineNumber, scriptPath, ex);
            }
        }
    }

    @Override
    protected void closeConnectionQuietly(Statement statement) {
        try {
            statement.close();
            connection.close();
        } catch (Exception e) {
            log.error("Could not close JDBC connection", e);
        }
    }
}

View on GitHub (pinned to 8e549514e3)