1. Overview
In this short tutorial, we’ll discuss what causes and what resolves the “Failed to configure a DataSource” error on a Spring Boot project.
We’ll resolve the issue using two different approaches:
- Defining the data source
- Disabling the auto-configuration of the data source
2. The Problem
Now, suppose we have a Spring Boot project, and we’ve added the spring-data-starter-jpa dependency and a MySQL JDBC driver to our pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
But, when we run the application, it fails with the error:
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
Let’s see why this is happening.
3. The Cause
By design, Spring Boot auto-configuration tries to configure the beans automatically based on the dependencies added to the classpath.
And, since we have the JPA dependency on our classpath, Spring Boot tries to automatically configure a JPA DataSource. The problem is, we haven’t given Spring the information it needs to perform the auto-configuration.
For example, we haven’t defined any JDBC connection properties, and we’ll need to do so when working with external databases like MySQL and MSSQL. On the other hand, we won’t face this issue with in-memory databases like H2 since they can create a data source without all this information.
4. Solutions
4.1. Define the DataSource Using Properties
Since the issue occurs due to the missing database connection, we can solve the problem simply by providing the data source properties.
First, let’s define the data source properties in the application.properties file of our project:
spring.datasource.url=jdbc:mysql://localhost:3306/myDb spring.datasource.username=user1 spring.datasource.password=pass spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Or, we can provide the data source properties in application.yml:
spring: datasource: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/myDb username: user1 password: pass
4.2. Define the DataSource Programmatically
Alternatively, we can define our data source programmatically, by using the utility builder class DataSourceBuilder. We need to provide the database URL, username, password, and the SQL driver information to create our data source:
@Configuration public class DatasourceConfig { @Bean public DataSource datasource() { return DataSourceBuilder.create() .driverClassName("com.mysql.cj.jdbc.Driver") .url("jdbc:mysql://localhost:3306/myDb") .username("user1") .password("pass") .build(); } }
In short, we can choose to use any one of the above options to configure a data source as per our requirements.
4.3. Exclude DataSourceAutoConfiguration
In the previous section, we fixed the issue by adding the data source properties to our project. But, how do we solve this if we’re not yet ready to define our data source?
Let’s see how to prevent Spring Boot from auto-configuring the data source.
The class DataSourceAutoConfiguration is the base class for configuring a data source using the spring.datasource.* properties.
Now, there are a few ways that we can exclude this from the auto-configuration.
First, we can disable the auto-configuration using the spring.autoconfigure.exclude property in our application.properties file:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Likewise, we can do the same using our application.yml file:
spring: autoconfigure: exclude: - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Or, we can use the exclude attribute on our @SpringBootApplication or @EnableAutoConfiguration annotation:
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
In all the above examples, we disabled the auto-configuration of the DataSource. And, this will not affect auto-configuring any other beans.
So, to sum up, we can use any one of the above methods to disable Spring Boot’s auto-configuration of the data source.
Ideally, we should provide the data source information and use the exclude option only for testing.
5. Conclusion
In this article, we’ve seen what causes the “Failed to configure a DataSource” error. First, we fixed the issue by defining the data source. Next, we discussed how to work around the issue without configuring the data source at all.
As always, the full code used in this article is available on GitHub.