testcontainers/testcontainers-java · error · ConnectionCreationException
Could create JDBC statement
Error message
Could create JDBC statement
What it means
ContainerLessJdbcDelegate's createNewConnection throws ConnectionCreationException when connection.createStatement() fails. This delegate runs SQL against an existing external database without a container, so it fails when the supplied JDBC connection cannot produce a Statement.
Solutions
- Verify the connection is open (connection.isClosed()) before using the delegate
- Re-create the connection via DriverManager before delegating
- Check the chained SQLException cause for the vendor error code
- Ensure single-threaded or synchronized use of the underlying connection
Example fix
// before new ContainerLessJdbcDelegate(closedConnection).execute(sql, ...); // after if (connection.isClosed()) connection = DriverManager.getConnection(url, user, pass); new ContainerLessJdbcDelegate(connection).execute(sql, ...);
Defensive patterns
Strategy: validation
Validate before calling
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection(url, user, password);
} Try / catch
try {
delegate.execute(statement, 0, null, null);
} catch (ConnectionCreationException e) {
log.error("Statement creation failed on external DB", e.getCause());
throw e;
} Prevention
- Check connection.isClosed() before creating the delegate
- Use a fresh connection per delegate instance
- Avoid sharing connections across threads
When it happens
Trigger: Using ContainerLessJdbcDelegate with a closed, invalid, or broken connection whose createStatement() raises SQLException.
Common situations: Connection already closed after pool eviction; connection created with a bad URL; reusing a connection across threads after it was invalidated.
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
- Could not obtain JDBC connection
- Could not obtain cassandra connection
- The ClickHouse does not support this
- Could not create new connection
- Database name not supported
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/d04399b1925f0a91.
Report an issue: GitHub.
Appendix: source
Thrown at modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerLessJdbcDelegate.java:33
* @see org.testcontainers.ext.ScriptUtils
*/
@Slf4j
public class ContainerLessJdbcDelegate extends JdbcDatabaseDelegate {
private Connection connection;
public ContainerLessJdbcDelegate(Connection connection) {
super(null, "");
this.connection = connection;
}
@Override
protected Statement createNewConnection() {
try {
return connection.createStatement();
} catch (SQLException e) {
log.error("Could create JDBC statement");
throw new ConnectionCreationException("Could create JDBC statement", e);
}
}
}
View on GitHub (pinned to 8e549514e3)