Skip to content

Support hikari data-source-properties in ContainerDatabaseDriver #1537

Description

@bojanv55

I tried to use test containers with hikari data-source-properties in Spring Boot like this:

spring:
  datasource:
    driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
    url: jdbc:tc:mysql:5.7.22://testcontainers/hi_perf_java_pers #?rewriteBatchedStatements=true&profileSQL=true&logger=com.mysql.cj.log.Slf4JLogger
    hikari:
      data-source-properties:
        rewriteBatchedStatements: true
        profileSQL: true
        logger: com.mysql.cj.log.Slf4JLogger

But datasource properties are ignored by connect method inside ContainerDatabaseDriver (info parameter in method connect).

Workaround so far is to include properties directly into connection string (commented part in url #?rewriteBatched...).

MySQL driver is doing it in this way:

/**
     * Builds a connection URL cache map key based on the connection string itself plus the string representation of the given connection properties.
     * 
     * @param connString
     *            the connection string
     * @param info
     *            the connection arguments map
     * @return a connection string cache map key
     */
    private static String buildConnectionStringCacheKey(String connString, Properties info) {
        StringBuilder sbKey = new StringBuilder(connString);
        sbKey.append("\u00A7"); // Section sign.
        sbKey.append(
                info == null ? null : info.stringPropertyNames().stream().map(k -> k + "=" + info.getProperty(k)).collect(Collectors.joining(", ", "{", "}")));
        return sbKey.toString();
    }

Activity

  1. changed the title [-]ContainerDatabaseDriver ignores data-source-properties[/-] [+]Support data-source-properties in ContainerDatabaseDriver[/+] on Jun 24, 2019
  2. changed the title [-]Support data-source-properties in ContainerDatabaseDriver[/-] [+]Support hikari data-source-properties in ContainerDatabaseDriver[/+] on Jun 24, 2019
  3. kiview commented on Jun 24, 2019

    @kiview
    Member

    You are right, I changed the phrasing of the title to make this more into a feature request.
    Would this be a Hikari specific feature?

  4. bojanv55 commented on Jun 24, 2019

    @bojanv55
    Author

    AFAIK this is JDBC feature.

  5. kiview commented on Jun 24, 2019

    @kiview
    Member

    I don't think there is a way to set generic properties on a JDBC connection, could you point me to the documentation?

    Also this is a Spring-Boot-Hikari specific thing IMO, so maybe we should check in the Spring-Boot code what happens with the Hikari object etc.

  6. bojanv55 commented on Jun 24, 2019

    @bojanv55
    Author

    smth like this?

    Properties props = new Properties();
    props.setProperty("user", "dbuser");
    props.setProperty("password", "dbpassword");
    props.setProperty(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CONNECT_TIMEOUT, "2000");
    
    Connection con = DriverManager.getConnection("<JDBC connection string>", props);
    
  7. kiview commented on Jun 25, 2019

    @kiview
    Member
  8. glefloch commented on Aug 28, 2019

    @glefloch
    Contributor

    Hi,
    I would like to contribute and fix this issue, but, I'm not sure on the fix.
    Can we agree that the modification here is to actually use info param from this method:

    public synchronized Connection connect(String url, final Properties info) throws SQLException {

    into that call :

    Connection connection = container.createConnection(queryString);

    which means updating this method signature (or adding a new one) to accept Properties info and merge username and password to it:

    public Connection createConnection(String queryString) throws SQLException, NoDriverFoundException {
    final Properties info = new Properties();
    info.put("user", this.getUsername());
    info.put("password", this.getPassword());

    Am I right?

    Do you know how could I test this ?

  9. s-jepsen commented on Sep 2, 2022

    @s-jepsen

    Any progress on this?

  10. hariohmprasath commented on Sep 19, 2022

    @hariohmprasath
    Contributor

    Hi @kiview,
    We have a similar requirement and based on my understanding we need to fix this in two places:

    Changes to cache URL

    We need to make sure the cache URL is formed using the properties, which would a change to ContainerDatabaseDriver.java.

    1. Reuse MySQL buildConnectionStringCacheKey() method to create the connection URL, based on URL + JDBC properties:
    private static String buildConnectionStringCacheKey(String connString, Properties info) {
            StringBuilder sbKey = new StringBuilder(connString);
            sbKey.append("\u00A7"); // Section sign.
            sbKey.append(
                    info == null ? null : info.stringPropertyNames().stream().map(k -> k + "=" + info.getProperty(k)).collect(Collectors.joining(", ", "{", "}")));
            return sbKey.toString();
    }
    1. Update the following line of code in ContainerDatabaseDriver.connect(String url, final Properties info) to create the cache URL based on (URL+Properties) instead of just URL:
    // New changes to build connection string based on (URL + connection properties) instead of just using the URL
    final String cacheUrl = buildConnectionStringCacheKey(url, info);
    ConnectionUrl connectionUrl = ConnectionUrl.newInstance(cacheUrl);

    Pass in properties to JDBC connector

    1. Method signature of JdbcDatabaseContainer.createConnection(String queryString) should get updated to JdbcDatabaseContainer.createConnection(String queryString, Properties info) in order to accept connection properties.
    2. Merge the passed in connection properties with the temp properties created inside the function so user defined JDBC properties gets sent in to the new connection object
    final Properties info = new Properties();
    info.put("user", this.getUsername());
    info.put("password", this.getPassword());

    Let me know your thoughts and if you are fine with this approach I can work on the changes and send in a PR shortly. Thanks.

  11. roulpriya commented on Oct 9, 2022

    @roulpriya
    Contributor

    Hi,
    I'm willing to work on this issue.

  12. Kamveno commented on Sep 26, 2023

    @Kamveno

    Hi, can you give me a chance to crack this?

  13. PSW99 commented on Apr 4, 2026

    @PSW99

    I'd like to work on this issue. I'll also address the related #5622. My approach would be to pass non-TC_ properties from the info parameter through to the delegate driver's connect() call. Does this sound reasonable

  14. chrc commented on Apr 29, 2026

    @chrc

    Any progress on this?

  15. Marcinnn123 commented on Dec 2, 2026

    @Marcinnn123

    Looked into this. The problem is that createConnection does new Properties(info), which turns info into just default values. Those are visible through getProperty() but not through plain get(), keySet(), or entrySet(), which is how a lot of drivers and pools actually read properties. PRs 11773 and 11947 already fix it the same way.

    I also wrote an end to end test with a real Postgres container and HikariCP data source properties that confirms the fix works for exactly the scenario in this issue. Happy to open a PR with it, or just add that test to one of the existing PRs instead, so we don't end up with a third duplicate attempt. Whatever works best for you.

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