diff --git a/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java b/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java index f5447f4010e..b2eefaf3f31 100644 --- a/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java +++ b/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java @@ -71,7 +71,7 @@ public SELF withUsername(String username) { throw new UnsupportedOperationException(); } - public SELF withPassword(String password){ + public SELF withPassword(String password) { throw new UnsupportedOperationException(); } @@ -127,8 +127,9 @@ public Driver getJdbcDriverInstance() { /** * Creates a connection to the underlying containerized database instance. * - * @param queryString any special query string parameters that should be appended to the JDBC connection URL. The - * '?' character must be included + * @param queryString + * query string parameters that should be appended to the JDBC connection URL. + * The '?' character must be included * @return a Connection * @throws SQLException if there is a repeated failure to create the connection */ @@ -136,7 +137,7 @@ public Connection createConnection(String queryString) throws SQLException { final Properties info = new Properties(); info.put("user", this.getUsername()); info.put("password", this.getPassword()); - final String url = appendDisableSslConfig(this.getJdbcUrl() + queryString); + final String url = constructUrlForConnection(queryString); final Driver jdbcDriverInstance = getJdbcDriverInstance(); @@ -147,9 +148,18 @@ public Connection createConnection(String queryString) throws SQLException { } } - private String appendDisableSslConfig(String connectionString){ - String separator = connectionString.contains("?") ? "&" : "?"; - return connectionString + separator + "useSSL=false"; + /** + * Template method for constructing the JDBC URL to be used for creating {@link Connection}s. + * This should be overridden if the JDBC URL and query string concatenation or URL string + * construction needs to be different to normal. + * + * @param queryString + * query string parameters that should be appended to the JDBC connection URL. + * The '?' character must be included + * @return a full JDBC URL including queryString + */ + protected String constructUrlForConnection(String queryString) { + return getJdbcUrl() + queryString; } protected void optionallyMapResourceParameterAsVolume(@NotNull String paramName, @NotNull String pathNameInContainer, @NotNull String defaultResource) { diff --git a/modules/mysql/src/main/java/org/testcontainers/containers/MySQLContainer.java b/modules/mysql/src/main/java/org/testcontainers/containers/MySQLContainer.java index 559586f7ec1..cb93fc5a7a1 100644 --- a/modules/mysql/src/main/java/org/testcontainers/containers/MySQLContainer.java +++ b/modules/mysql/src/main/java/org/testcontainers/containers/MySQLContainer.java @@ -54,6 +54,12 @@ public String getJdbcUrl() { return "jdbc:mysql://" + getContainerIpAddress() + ":" + getMappedPort(MYSQL_PORT) + "/" + databaseName; } + @Override + protected String constructUrlForConnection(String queryString) { + String separator = queryString.contains("?") ? "&" : "?"; + return getJdbcUrl() + separator + "useSSL=false"; + } + @Override public String getDatabaseName() { return databaseName;