close

[Solved] After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName Error Occurs?

Today I am facing the following error After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName in Java.

How To Solve After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName Error ?

  1. How To Solve After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName Error ?

    To Solve After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName Error There is also how-to in the docs how to get it working from “both worlds”. It would look like below. ConfigurationProperties bean would do “conversion” for jdbcUrl from app.datasource.url

  2. After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName

    To Solve After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName Error There is also how-to in the docs how to get it working from “both worlds”. It would look like below. ConfigurationProperties bean would do “conversion” for jdbcUrl from app.datasource.url

Solution 1

As this post gets a bit of popularity I edited it a bit. Spring Boot 2.x.x changed default JDBC connection pool from Tomcat to faster and better HikariCP. Here comes incompatibility, because HikariCP uses different property of jdbc url. There are two ways how to handle it:

OPTION ONE

There is very good explanation and workaround in spring docs:

Also, if you happen to have Hikari on the classpath, this basic setup does not work, because Hikari has no url property (but does have a jdbcUrl property). In that case, you must rewrite your configuration as follows:

app.datasource.jdbc-url=jdbc:mysql://localhost/test
app.datasource.username=dbuser
app.datasource.password=dbpass

OPTION TWO

There is also how-to in the docs how to get it working from “both worlds”. It would look like below. ConfigurationProperties bean would do “conversion” for jdbcUrl from app.datasource.url

@Configuration
public class DatabaseConfig {
    @Bean
    @ConfigurationProperties("app.datasource")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("app.datasource")
    public HikariDataSource dataSource(DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().type(HikariDataSource.class)
                .build();
    }
}

Solution 2

In case you do need to define dataSource(), for example when you have multiple data sources, you can use:

@Autowired Environment env;

@Primary
@Bean
public DataSource customDataSource() {

    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getProperty("custom.datasource.driver-class-name"));
    dataSource.setUrl(env.getProperty("custom.datasource.url"));
    dataSource.setUsername(env.getProperty("custom.datasource.username"));
    dataSource.setPassword(env.getProperty("custom.datasource.password"));

    return dataSource;

}

By setting up the dataSource yourself (instead of using DataSourceBuilder), it fixed my problem which you also had.

The always knowledgeable Baeldung has a tutorial which explains in depth.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read