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

If the spring-boot-starter-data-jpa dependency is added in the spring boot pom.xml file, when the spring boot application starts the exception “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” will be thrown. In this article, we will see what causes this exception, How this exception can be fixed.

Error Message

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


How to reproduce this exception

In the spring boot application, add spring-boot-starter-data-jpa dependency in the pom.xml file. Restart the spring boot application. This exception will be thrown at the start of the application.

Add the following spring-boot-starter-data-jpa dependency in your pom.xml.

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

The following exception will be thrown in the console when the application is started.

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


Root Cause

The JPA (Java persistence API) is a java specification for ORM (Object-Relational Mapping) tools. The spring-boot-starter-data-jpa dependency enables ORM in the context of the spring boot framework.

The JPA auto configuration feature of the spring boot application attempts to establish database connection using JPA Datasource. The JPA DataSource bean requires database driver to connect to a database.

The database driver should be available as a dependency in the pom.xml file. For the external databases such as Oracle, SQL Server, MySql, DB2, Postgres, MongoDB etc requires the database JDBC connection properties to establish the connection.

The in-memory databases such as H2, HSQL, Derby etc will establish a connection without JDBC connection properties as it is part of the spring boot application.

You need to configure the database driver and the JDBC connection properties to fix this exception.



Solution 1 – In Memory Database

The in-memory databases such as H2, HSQL, Derby etc do not need JDBC connection properties. Add the corresponding database dependency in the pom.xml file. The code below shows the dependency for the in-memeory databases

H2 Database

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
  	
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>

HSQL Database

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>org.hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<scope>runtime</scope>
		</dependency>

Derby Database

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		
		<dependency>
		    <groupId>org.apache.derby</groupId>
		    <artifactId>derby</artifactId>
		    <scope>runtime</scope>
		</dependency>

Note : For Derby Database, add the below configuration in application.properties to avoid the schema error.

spring.jpa.hibernate.ddl-auto=update


Solution 2 – RDBMS Database

The RDBMS databases such as Oracle, SQL Server, My SQL, Postgres etc need JDBC connection properties. Add the corresponding database dependency in the pom.xml file. Add the data source properties in application.properties or application.yml file. The code below shows the dependency for the RDBMS databases

MySql Database

		<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>
spring.datasource.url=jdbc:mysql://localhost/testdb
spring.datasource.username=root
spring.datasource.password=rootpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

Oracle Database

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc7</artifactId>
			<version>12.1.0.1</version>
		</dependency>
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

SQL Server Database

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>com.microsoft.sqlserver</groupId>
			<artifactId>mssql-jdbc</artifactId>
			<scope>runtime</scope>
		</dependency>
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testdb
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

Postgres Database

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</dependency>        
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update


Solution 3 – NoSQL Databases

The NoSQL databases such as MongoDB etc need JDBC connection properties. Add the corresponding database dependency in the pom.xml file. Add the data source properties in application.properties or application.yml file. The code below shows the dependency for the NoSQL databases

MongoDB

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>

application.properties

spring.data.mongodb.uri=mongodb://root:rootpassword@localhost:27017/testdb


Solution 4 – Exclude JPA from pom.xml

If you are not using any database in spring boot application, delete the spring-boot-starter-data-jpa dependency from the pom.xml. The spring-boot-starter-data-jpa dependency attempts to create a connection to the database. This error is not seen because the module is excluded from the file 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>


Solution 5 – Exclude auto-configuration

The JPA auto configuration feature of the spring boot application attempts to establish database connection using JPA Datasource. The java class DataSourceAutoConfiguration is responsible for loading JPA DataSource. The auto configuration can be disabled by excluding DataSourceAutoConfiguration class from the spring boot context.

There are three ways to exclude the DataSourceAutoConfiguration class from the spring boot context. Use the main class annotation @SpringBootApplication or @EnableAutoConfiguration with exclude attribute, to disable the auto configuration.

package com.yawintutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration;

@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
public class SpringBootMySqlApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootMySqlApplication.class, args);
	}

}

Use the spring.autoconfigure.exclude property in application.properties file or application.yml file to disable the auto configuration class DataSourceAutoConfiguration.

application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

application.yml

spring:
	autoconfigure:
		exclude:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration    


Summary

In this post we saw the details of the exception “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”, The root cause of the exception, The ways to fix this exception.

This exception will be thrown due to the developer tries to configure the database configuration. The database driver dependency or the JDBC configuration is missed in most of the cases.



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *