Search code examples
javatomcatjdbccloud-foundryspring-cloud

Cannot set connectionProperties with Spring Cloud Connectors in Cloud Foundry


I am setting up my DataSource in a Spring Boot / Spring Cloud Connectors project running on Cloud Foundry using Tomcat JDBC Connection Pool and MariaDB JDBC driver like so:

@Configuration
@Profile("cloud")
public class MyDataSourceConfiguration extends AbstractCloudConfig {
    @Bean
    public DataSource dataSource() {
        Map<String, Object> dataSourceProperties = new HashMap<>();
        dataSourceProperties.put("initialSize", "4"); // OK
        dataSourceProperties.put("maxActive", "4");   // OK
        dataSourceProperties.put("maxWait", "2000");  // OK
        dataSourceProperties.put("connectionProperties",      
        "useUnicode=yes;characterEncoding=utf8;"); // ignored
        DataSourceConfig conf = new DataSourceConfig(dataSourceProperties);
        return connectionFactory().dataSource(conf);
    }
}

For some reason only the properties referring to the pool size and maxWait but not the connectionProperties are getting picked up by the DataSource bean - see the log output:

maxActive=4; initialSize=4; maxWait=2000; connectionProperties=null

Any hints ?

Note: Trying to set the connectionProperties via Spring's ConnectionConfig class didn't work either.


Solution

  • Try using the form of DataSourceConfig that takes separate PoolConfig and ConnectionConfig beans, like this:

    @Bean
    public DataSource dataSource() {
        PoolConfig poolConfig = new PoolConfig(4, 4, 2000);
        ConnectionConfig connectionConfig = new ConnectionConfig("useUnicode=yes;characterEncoding=utf8;");
        DataSourceConfig dbConfig = new DataSourceConfig(poolConfig, connectionConfig);
        return connectionFactory().dataSource(dbConfig);
    }