2

Why is the driverclass missing in the following configuration?

spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable
spring.datasource.testdb.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.testdb.username=test
spring.datasource.testdb.password=test


@Configuration
public class DataSourceConfig {
    @ConfigurationProperties(prefix = "spring.datasource.testdb")
    @Primary
    public DataSource dataSourceTest() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public JdbcTemplate jdbcTemplateTest() {
        return new JdbcTemplate(dataSourceTest());
    }

    //secondary db config to follow
}

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
    </dependency>
</dependencies>

Result:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class

This is strange, because I can even step into the org.mariadb.jdbc.Driver class, so it is clearly on classpath.

membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

2

Looks like you just need to add the @Bean annotation to dataSourceTest().

@Bean
@ConfigurationProperties(prefix = "spring.datasource.testdb")
@Primary

Also, for the Hikari connection pool (which is the default connection pool), the url property is jdbc-url, not url. So change

spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable

to

spring.datasource.testdb.jdbc-url=jdbc:mariadb://localhost/mytable

For more info, and other possible solutions see: After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName

Hope this helps.

Ryan Stuetzer
  • 392
  • 1
  • 4
  • I tried, and the app then starts successfully. BUT on access of `JdbcTemplate` I then get: *Exception in thread "main" java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.* – membersound Jun 18 '19 at 15:19
  • I modified the answer to also change the url property to jdbc-url. – Ryan Stuetzer Jun 18 '19 at 15:47
  • 1
    Indeed especially the following answer seems to provide the solution: https://stackoverflow.com/a/49141541/1194415 – membersound Jun 18 '19 at 19:05
0

Are you sure that the properties file is being loaded correctly? The message clearly specifies that the url attribute was not specified, so apparently the spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable line is not being read - the driver classpath has nothing to do with it.

The lines should be in an application.properties file by default, is that where you have them?

Piotr Wilkin
  • 3,446
  • 10
  • 18
  • Yes it's in `application.properties`. If I remove the `DataSourceConfig` and change the configuration to one primary ds only, eg `spring.datasource.url=jdbc:mariadb://localhost/mytable`, everything works as expected. So in general the setup should be fine. Just the mutiple ds config not... – membersound Jun 18 '19 at 14:59