Failed to configure a DataSource url attribute is not specified and no embedded datasource could be configured

In this post, we will see how to fix Failed to configure a DataSource url attribute is not specified and no embedded datasource could be configured.

We might get the below exception while running the spring boot application.

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).

The error message “Failed to configure a DataSource: ‘url’ attribute is not specified, and no embedded datasource could be configured” typically occurs in Java applications when you’re using a framework like Spring Boot and trying to set up a database connection, but the application doesn’t know how to configure the data source properly. This error suggests that you haven’t provided the necessary configuration for your data source.

Let’s see the code that is responsible for this exception.

The DataSourceProperties.java class from org.springframework.boot.autoconfigure.jdbc package has initializeDataSourceBuilder() method that is used to determine driver class, URL, and password.

	public DataSourceBuilder<?> initializeDataSourceBuilder() {
		return DataSourceBuilder.create(getClassLoader()).type(getType()).driverClassName(determineDriverClassName())
				.url(determineUrl()).username(determineUsername()).password(determinePassword());
	}

How to fix Failed to configure a DataSource url attribute is not specified and no embedded datasource could be configured exception

1. Using @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

If we are not using any database for our spring boot application, we might consider excluding auto datasource configuration using exclude = {DataSourceAutoConfiguration.class} in our @SpringBootApplication annotation, we are telling Spring Boot not to automatically configure a data source. In other words, we are disabling the default behavior of setting up a database connection based on the data source properties

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class SpringMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringMain.class, args);

    }
}

2. If we are using database for Spring Boot application, we need to provide proper database details in appliation.properties file

application.properties file for Spring Boot Mysql configuration.

spring.datasource.url=jdbc:mysql://localhost:3306/springbootcrudexample
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
server.port = 9091

Note – if you are using spring.datasource.driverClassName=com.mysql.jdbc.Driver then change it to spring.datasource.driver-class-name=com.mysql.jdbc.Driver

application.properties file for Spring Boot Oracle configuration

# Connection url for the database
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.username=SYSTEM
spring.datasource.password=oracle
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql=true 
 
spring.jpa.hibernate.ddl-auto =create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
server.port = 9091
#show sql values
#logging.level.org.hibernate.type.descriptor.sql=trace

hibernate.show_sql = true
#spring.jpa.hibernate.logging.level.sql =FINE
#show sql statement
#logging.level.org.hibernate.SQL=debug

application.properties file for Spring Boot PostgreSQL configuration.

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
server.port = 9091
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

application.properties file for h2 database

# H2 Database Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password

# H2 Console Configuration (optional)
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

3. Define spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration in application.properties file.

4. Make sure we have the appropriate database driver dependency in our project. If we are using Maven, we can include it in our pom.xml.

<dependencies>
    <!-- Other dependencies -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.27</version> <!-- Use the appropriate version -->
    </dependency>
</dependencies>

5. Verify that our application is loading the correct configuration file. Spring Boot typically looks for application.properties or application.yml in the classpath by default. Make sure you haven’t misspelled the filename or placed it in the wrong location.

6. Make sure that our database server is up and running, and the provided URL is correct.

That’s all about Failed to configure a DataSource url attribute is not specified and no embedded datasource could be configured

Other posts.