testcontainers/testcontainers-java · error · ConnectionCreationException

Could not obtain cassandra connection

Error message

Could not obtain cassandra connection

What it means

CassandraDatabaseDelegate.createNewConnection builds a CQL Session from the container's cluster via CassandraContainer.getCluster(container). If the DataStax driver throws a DriverException (contact point unreachable, protocol/auth failure, node not ready), it logs 'Could not obtain cassandra connection' and throws ConnectionCreationException.

Solutions

  1. Wait for the container to accept connections (proper wait strategy) before executing queries
  2. Verify the container is running and the mapped CQL port: container.getMappedPort(9042)
  3. Check image version compatibility with the bundled DataStax driver; use a supported Cassandra image
  4. Inspect the wrapped DriverException cause for auth/protocol details
Defensive patterns

Strategy: retry

Validate before calling

// Ensure the container is running and CQL port is mapped before querying
if (!container.isRunning()) throw new IllegalStateException("container not started");
container.getMappedPort(9042);

Try / catch

try { delegate.execute("SELECT now() FROM system.local", 1, "probe", null, false, false); } catch (ConnectionCreationException e) { /* check DriverException cause, backoff-retry */ }

Prevention

When it happens

Trigger: Any execute() call on CassandraDatabaseDelegate (used by CassandraQueryWaitStrategy or runInitScriptIfRequired) when cluster.connect()/newSession() raises DriverException.

Common situations: Querying too early before Cassandra is listening on 9042; driver/container CQL protocol version mismatch; auth credentials configured in the image but not in the delegate; network/NAT issues on non-Linux Docker setups.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


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

Appendix: source

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

/**
 * Cassandra database delegate
 *
 * @deprecated use {@link org.testcontainers.cassandra.CassandraDatabaseDelegate} instead.
 */
@Slf4j
@RequiredArgsConstructor
@Deprecated
public class CassandraDatabaseDelegate extends AbstractDatabaseDelegate<Session> {

    private final ContainerState container;

    @Override
    protected Session createNewConnection() {
        try {
            return CassandraContainer.getCluster(container).newSession();
        } catch (DriverException e) {
            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);
            }

View on GitHub (pinned to 8e549514e3)