In this article, we’ll talk about the spring boot error Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured. This error occurs because the datasource configuration for the application is missing. The problem can be resolved Using two approaches, datasource configuration and disabling datasource auto configuration.
How to reproduce this problem
Create a spring boot application and include two maven dependencies, Spring Data JPA and MySQL Driver. The pom.xml file will contain the two dependencies spring-boot-starter-data-jpa and mysql-connector-java after you create the spring boot application. Run the spring boot application now. The error message will be displayed in the console window.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
The error message Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured will appear in the console window, as shown below.
***************************
APPLICATION FAILED TO START
***************************
Description:
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
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Root Cause
The spring boot application has JPA configuration enabled. The spring boot application’s auto configuration will attempt to connect to the database. Datasource configurations such as database url, user name, password, database driver name, and so on are required to establish the database connection. The spring boot application searches configuration files such as application.properties and application.yaml for the datasource configuration. The file does not contain any datasource configurations. As a result, the spring boot application will throw the error Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
Solution 1
In the application.properties file, the datasource configuration is missing. To resolve this error, add the datasource configuration to the application.properties file. In the application.properties file, add the configuration properties listed below.
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Solution 2
If you’re using the application.yaml file, add the following configuration to it. The spring boot application will look in the application.properties file first, then in the application.yaml file.
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/testdb
username: root
password: password
Solution 3
Java code can be used to configure the datasource. The spring boot application framework will allow the datasource to be configured in the java files. The datasource configuration in java code is shown in the example below.
package com.yawintutor;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DataSourceConfiguration {
@Bean
public DataSource getDataSource() {
return DataSourceBuilder.create()
.driverClassName("com.mysql.cj.jdbc.Driver")
.url("jdbc:mysql://localhost:3306/testdb")
.username("root")
.password("password")
.build();
}
}
Solution 4
The DataSourceAutoConfiguration is in charge of checking and connecting the database to the datasource. Exclude the configuration in the application.properties file to disable DataSourceAutoConfiguration.
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Soution 5
DataSourceAutoConfiguration can be disabled by excluding it from the application.yaml file. To disable auto configuration, add the following yaml configuration to the spring boot application.yaml file.
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Solution 6
DataSourceAutoConfiguration can also be disabled in the java file. Add the exclude configuration code to the spring boot application main class to disable auto configuration.
package com.yawintutor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class SpringBootJpaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJpaApplication.class, args);
}
}