Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


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:

  1. Defining the data source
  2. Disabling the auto-configuration of the data source

Further reading:

Configuring a DataSource Programmatically in Spring Boot

Learn how to configure a Spring Boot DataSource programmatically, thereby side-stepping Spring Boot's automatic DataSource configuration algorithm.

Configuring Separate Spring DataSource for Tests

A quick, practical tutorial on how to configure a separate data source for testing in a Spring application.

Spring Boot With H2 Database

Learn how to configure and how to use the H2 database with Spring Boot.

2. The Problem

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:


But when we run the application, it fails with this error:


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 that 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 such as MySQL and MSSQL. On the other hand, we won't face this issue with in-memory databases such as 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:


Or we can provide the data source properties in application.yml:

    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:

public class DatasourceConfig {
    public DataSource datasource() {
        return DataSourceBuilder.create()

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:


And we can do the same using our application.yml file:

    - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Or we can use the exclude attribute on our @SpringBootApplication or @EnableAutoConfiguration annotation:


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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


Persistence footer banner
Comments are closed on this article!