Skip to content

databasename part of jdbc:tc URIs іs ignored when optional host:port pair is omitted #4121

Open

Description

@Alexander-Kiselyov

Note that this issue is related to behavior mentioned in #1846.

When one uses "host-less URI" as specified on the JDBC support page, e.g.: jdbc:tc:mysql:5.7.12:///mydatabase, DB name is also ignored and a default test name is used instead.
This code fragment from org.testcontainers.jdbc.ConnectionUrl#parseUrl method clearly shows how it is handled:

//In case it matches to the default pattern
Matcher dbInstanceMatcher = Patterns.DB_INSTANCE_MATCHING_PATTERN.matcher(dbHostString);
if (dbInstanceMatcher.matches()) {
	databaseHost = Optional.of(dbInstanceMatcher.group(1));
        databasePort = Optional.ofNullable(dbInstanceMatcher.group(3)).map(value -> Integer.valueOf(value));
        databaseName = Optional.of(dbInstanceMatcher.group(4));
}

then org.testcontainers.containers.JdbcDatabaseContainerProvider#newInstanceFromConnectionUrl defaults DB name to test:

final String databaseName = connectionUrl.getDatabaseName().orElse("test");

In my particular case an init SQL script, which starts with the following instruction, fails with Caused by: java.sql.SQLSyntaxErrorException: Access denied for user 'test'@'%' to 'mydatabase' error:

CREATE DATABASE IF NOT EXISTS `mydatabase`;

Aforementioned documentation page says:

Note that the hostname, port and database name will be ignored; you can leave these as-is or set them to any value.

which apparently is not the case and /// syntax, which includes DB name only, is recommended and multiple examples are provided.

I suggest to improve "host-less" URI handling by honoring DB name and clarify the documentation on this matter. Will make a PR for this if project owners are OK with such resolution.

Activity

  1. changed the title [-]`databasename` part of jdbc:tc URI іs ignored when optional host:port pair is omitted[/-] [+]`databasename` part of `jdbc:tc` URIs іs ignored when optional host:port pair is omitted[/+] on May 24, 2021
  2. chadlwilson commented on Oct 14, 2021

    @chadlwilson
    Contributor

    I just came across this too, with both 1.15.3 and 1.16.0.

    Given it contradicts the documentation and the /// syntax works fine for r2dbc connections, I think this is a bug in the URL parsing?

    There are also no tests for such URLs at https://github.com/testcontainers/testcontainers-java/blob/3aa5fbfa17be09f4e42b2300396890f76f508c86/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java

    This seems to be a re-prod:

    import org.junit.jupiter.api.Test;
    import org.testcontainers.jdbc.ConnectionUrl;
    
    import static org.assertj.core.api.Assertions.assertThat;
    
    public class ConnectionUrlTest {
        @Test
        void shouldParseDatabaseNameWithoutHostPort() {
            var url = ConnectionUrl.newInstance("jdbc:tc:mysql:8:///databasename");
            assertThat(url.getDatabaseType()).isEqualTo("mysql");
            assertThat(url.getImageTag()).hasValue("8");
            assertThat(url.getDatabaseHost()).isEmpty();
            assertThat(url.getDatabasePort()).isEmpty();
            assertThat(url.getDatabaseName()).hasValue("databasename"); // FAILS
        }
    }

    Other than specifying host/port, I found another workaround was just to add a fourth / which causes it to put / in databaseHost which is subsequently ignored by testcontainers anyway.

  3. chadlwilson commented on Oct 14, 2021

    @chadlwilson
    Contributor

    Seems this is a dupe of #2544 which keeps getting closed for staleness.

  4. kiview commented on Oct 14, 2021

    @kiview
    Member

    Let's keep this issue open, thanks for providing the reproducer @chadlwilson and thanks @Alexander-Kiselyov for the detailed investigation.

    Before we invest any work on this issue, is there a good reason to use the host-less URI format? Bringing the docs in line with the implementation is of course critical.

  5. chadlwilson commented on Oct 14, 2021

    @chadlwilson
    Contributor

    From my perspective, I think it's less confusing when reading one's config to have host-less URIs because you don't have to see a misleading localhost:3306 which is going to be thrown away/ignored by testcontainers anyway. Probably makes the docs a bit cleaner to read where there are a lot of different examples for different DBs as well.

    That's the justification in the current docs as well:

    We will use /// (host-less URIs) from now on to emphasis the unimportance of the host:port pair.
    From Testcontainers' perspective, jdbc:mysql:5.7.34://localhost:3306/databasename and jdbc:mysql:5.7.34:///databasename is the same URI.

  6. eschulma commented on Jan 30, 2023

    @eschulma

    I like the host-less URIs as well, but until this is fixed please update the documentation. I wasted a day on this.

  7. odrotbohm commented on Jul 9, 2024

    @odrotbohm

    Is there any reason that this is not approached (either by fixing the implementation and/or the docs)? I just spent half a day trying to find out why test was used instead of the URL-defined database name. 😕

  8. linghengqian commented on Dec 23, 2025

    @linghengqian
    Contributor

    In fact, my tests showed that testcontainers-postgresql, testcontainers-mssqlserver, testcontainers-clickhouse, and even the external org.firebirdsql:firebird-testcontainers-java did not have this problem.

    This issue only occurs in the Maven module of testcontainers-mysql.

  9. eschulma commented on Dec 23, 2025

    @eschulma

    So will it be fixed?

  10. chadlwilson commented on Dec 23, 2025

    @chadlwilson
    Contributor

    Seems like a perfectly good candidate for an open source pull request, for those it is important to - especially given there are simple enough reproducers.

  11. eschulma commented on Dec 23, 2025

    @eschulma

    Seems like a perfectly good candidate for an open source pull request, for those it is important to - especially given there are simple enough reproducers.

    Fair, and I have submitted pull requests to various other projects in the past. We are not currently using this library however, so it won’t be me for anyone reading.

  12. sharanggupta commented on Oct 14, 2026

    @sharanggupta

    For triage: this looks fixed by #10603 ("Fix support to set a custom database name in Host-less JDBC urls"), released in 2.0.0. On main, ConnectionUrl parses jdbc:tc:mysql:5.7.12:///mydatabase (and the same with a query string) to database name mydatabase with no host, so the provider no longer falls back to test. The issue can probably be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions