Jeff Cook
Jeff Cook

Reputation: 8774

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

I am working on a Spring Boot Batch example with MongoDB and I have already started the mongod server.

When I launch my application, I am getting the error below.

Any pointers for this issue?

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

application.properties:

# Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.uri=mongodb://localhost/test 

pom.xml

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

I have started mongod with the following output:

C:\Users\pc>mongod
2018-07-07T14:39:39.223+0530 I JOURNAL  [initandlisten] journal dir=C:\data\db\journal
2018-07-07T14:39:39.230+0530 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2018-07-07T14:39:39.478+0530 I JOURNAL  [durability] Durability thread started
2018-07-07T14:39:39.589+0530 I CONTROL  [initandlisten] MongoDB starting : pid=11992 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-NQ639DU
2018-07-07T14:39:39.589+0530 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-07-07T14:39:39.591+0530 I CONTROL  [initandlisten] db version v3.0.5
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] git version: 8bc4ae20708dbb493cb09338d9e7be6698e4a3a3
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] allocator: tcmalloc
2018-07-07T14:39:39.593+0530 I CONTROL  [initandlisten] options: {}
2018-07-07T14:39:39.595+0530 I JOURNAL  [journal writer] Journal writer thread started
2018-07-07T14:39:40.485+0530 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-07-07T14:40:39.140+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51340 #1 (1 connection now open)
2018-07-07T14:40:41.663+0530 I NETWORK  [conn1] end connection 127.0.0.1:51340 (0 connections now open)
2018-07-07T14:45:12.421+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51578 #2 (1 connection now open)
2018-07-07T14:45:12.870+0530 I NETWORK  [conn2] end connection 127.0.0.1:51578 (0 connections now open)
2018-07-07T14:46:21.734+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51591 #3 (1 connection now open)
2018-07-07T14:46:22.041+0530 I NETWORK  [conn3] end connection 127.0.0.1:51591 (0 connections now open)
2018-07-07T14:57:47.523+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:52534 #4 (1 connection now open)
2018-07-07T14:57:47.910+0530 I NETWORK  [conn4] end connection 127.0.0.1:52534 (0 connections now open)

enter image description here

Upvotes: 382

Views: 1301240

Answers (30)

mzhehalo
mzhehalo

Reputation: 98

In my case i changed file application-dev.yml. 1. Removed single quotes 2. Deleted null from datasource and jpa Screenshot Local History

Upvotes: 0

ReemRashwan
ReemRashwan

Reputation: 371

As a podman user (docker alternative), this error happened to me when I forget to feed the application with the .env file.

application.properties:

 spring.datasource.url= ${SPRING_DATASOURCE_URL}
 spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
 spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}

Dockerfile contains (note the extra = as it doesn't work without it)

ENV SPRING_DATASOURCE_URL=
ENV SPRING_DATASOURCE_USERNAME=
ENV SPRING_DATASOURCE_PASSWORD=

.env file

SPRING_DATASOURCE_URL=<yourDBUrl>
SPRING_DATASOURCE_USERNAME=<yourUsername>
SPRING_DATASOURCE_PASSWORD=<yourPassword>

Pass the .env file while running the container (what solved this error):

podman run --env-file .env -p 8082:8080 -d --name imageName containerName

Upvotes: 0

Ayush Choudhary
Ayush Choudhary

Reputation: 341

Clear dataSource files/folders from .idea folder if you are using IntelliJ Idea, sometimes it may take Database connection from some inbuilt DB connection maker which might be a problem !

Upvotes: 1

Mama africa
Mama africa

Reputation: 77

executing mvn clean command worked in my case.

Upvotes: 0

Sairam Pabbisetty
Sairam Pabbisetty

Reputation: 61

Adding the "scope" attribute in the mysql dependency in pom.xml file

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

and

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

in application.properties file worked for me in solving this error.

Upvotes: 1

jarosik
jarosik

Reputation: 4564

check your application.properties

changing

spring.datasource.driverClassName=com.mysql.jdbc.Driver

to

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

worked for me. Full config:

spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=
spring.datasource.password=   
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update

Upvotes: 206

Karpinski
Karpinski

Reputation: 157

In my case I wasn't using dataSource and had the SpringData dependency in the pom.xml configured. I just removed that dependency and it works;

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

Upvotes: 1

RCaetano
RCaetano

Reputation: 673

In our case we're getting the same error because our application couldn't connect to our Spring Cloud Config Server to fetch the configurations. We tested it locally with the default application.yml resources file and only when we deployed we noticed we forgot to add the required dependencies for in the POM file:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>${spring-cloud-starter-config.version}</version>
    </dependency>

...

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

After that everything started working as expected!

Upvotes: 0

Mohit Sehgal
Mohit Sehgal

Reputation: 332

It is strange but in my case I restarted STS and then I deleted the static and template folder from src/main/resources/ and then the application starts running.

Upvotes: 0

camilajenny
camilajenny

Reputation: 5034

For me, it simply didn't see the application.yml so I added --spring.config.location=/absolute/path/to/my/application.yml as CLI arguments to my application (program arguments) in my run configuration and it worked.

The reason was probably wrong module configuration with unnecessary JPA provider descriptor that I don't have in the project at all but there was a little pop up that I clicked unaware and I selected the source, test and resource directories on my own because the auto process didn't pop out

IntelliJ module breakdown IntelliJ project directories breakdown

Upvotes: 0

Armen Arzumanyan
Armen Arzumanyan

Reputation: 2043

Format application.yml https://jsonformatter.org/yaml-formatter or use application.properties

Upvotes: 0

JRichardsz
JRichardsz

Reputation: 16515

The following worked for 2021 spring-boot release 2.5.0

If you have as minimum these entries in your application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

And these dependencies in your pom.xml

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

You should not have 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

It was the IDE in my case

No matter if you are using eclipse or intellij, application must run over linux in real environments. So to validate if is an IDE problem, run your app using shell

mvn spring-boot:run

If it starts without error, the problem is in your IDE

Eclipse

Eclipse IDE for Enterprise Java and Web Developers
Version: 2021-03 (4.19.0)
Build id: 20210312-0638

In my case I was running the project with right click on the classic Application.java inside of spring boot project , then run as java application

After hours of researching, the solution was:

  • right click on the root spring boot project
  • then run as java application.
  • Eclipse shows several class with main methods.
  • I chose my Application.java and then run

Long Explanation

If you check the spring boot source code DataSourceProperties.determineDriverClassName you will see that just driverClassName or dirver-class-name and url is required. If not, the exception is thrown

Upvotes: 10

Avijit Nagare
Avijit Nagare

Reputation: 8782

This is an issue in new STS. STS couldn't read your application.yml/properties file. to fixed it follow below steps.

  1. Select project -> Right click and Select "Run as" -> "Run configuration" -> Select "Classpath" tab.

  2. Select "User entries" -> select "Advanced" button. -> Add folder -> and select "resources" under you project hierarchy (project->src->main->resources).

  3. It should be as per below screenshot.

enter image description here

Upvotes: 2

alexandre marillesse
alexandre marillesse

Reputation: 85

i had this problem because i used MongoDb ,with gradle 7.2.If there are people in the same case. you have to delete the dependency in your build.gradle:

"implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'"

Upvotes: 0

UmaShankar
UmaShankar

Reputation: 223

I have added spring.datasource.driver-class-name=org.postgresql.Driver in my application.properties then it worked

Upvotes: 0

Velmurugan A
Velmurugan A

Reputation: 446

There may be chance where you missed to mention your application profile in the VM arguments.

My case, I have missed to add below one in the VM arguments.

-Dspring.profiles.active=dev

Upvotes: 2

Kumar Anand
Kumar Anand

Reputation: 3685

Just add : @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }) works for me.

I was getting same error I tried with @EnableAutoConfiguration(exclude=...) didn't work.

For those that are wondering where to add @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }), as it has been asked by user as well. You need to add it to the main Application class which is under src>main>java. By default it is set to @SpringBootApplication

Upvotes: 364

cng.buff
cng.buff

Reputation: 575

That mean you lack "Define the DataSource Using Properties"

For define example :(remember change sql datasource, username and pass)

spring.datasource.url=jdbc:mysql://localhost:3306/basic_springboot 
spring.datasource.username=root
spring.datasource.password=manhcong
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

And you can ref this

https://www.baeldung.com/spring-boot-failed-to-configure-data-source

Upvotes: 0

Ever Rosales
Ever Rosales

Reputation: 31

An optiona Solution if already tested all last answers

If you have an error like title

And validated .properties connection string is correct.

Then add this code block about maven plugin in your .pom file

        ...
    </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-resources-plugin</artifactId>
               <version>3.1.0</version>
        </plugin>

...

And Update project. Works for me!!!

I hope this help you to.

This steps you the found in this link page.

Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources

https://exerror.com/failed-to-execute-goal-org-apache-maven-pluginsmaven-resources-plugin3-2-0resources/

And here: Maven clean install: Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources

Upvotes: 3

Ankit
Ankit

Reputation: 139

Step 1) Put following dependencies in pom.xml file.

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

Step 2) Put following code in application.properties file.

spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory? 
 useSSL=false&allowPublicKeyRetrieval=true
 spring.datasource.username=springstudent
  spring.datasource.password=springstudent

Step 3) Check carefully if application.properties file is placed in src/main/resources folder.

Step 4) Step 3 is very crucial as, you could spend all day in finding error in code, but the real problem is the location of the application.properties file.

Upvotes: 0

sifr_dot_in
sifr_dot_in

Reputation: 3593

every thing is fine when i run the project in IDE / STS (spring tool suit).
but this was thrown when i made a jar.

unnecessary spaces " " in the "application.yml" file can cause this.

server:
  port: 8085


spring:
 datasource:
  url: jdbc:mysql://localhost:3306/studentdb
  username: root
  password: root
  driver-class-name: com.mysql.cj.jdbc.Driver
 jpa:
   hibernate:
    ddl-auto: update
   show-sql: true
   database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
 application:
  name: STUDENT-SERVICE

instead of tweaking my "application.yml" file
i simply moved all my statements in "application.yml" file to
"application.properties" file and formatted the statements like required in ".properties".

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/studentdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format.sql=true

spring.application.name=student-service

server.port=8085

(you can add params at the end of url)
(spring.datasource.url=jdbc:mysql://localhost:3306/studentdb?allowPublicKeyRetrieval=true&useSSL=false)

and voilà

Upvotes: 0

Vikas Prajapat
Vikas Prajapat

Reputation: 11

Check spring profile also, by default it goes for 'default' profile, if your application properties have different profiles like the test, prod, etc then you need to set up it. for eclipse set environment variable as name=spring.profiles.default, value=test

Upvotes: 1

faruk13
faruk13

Reputation: 1345

For anyone using Spring Boot 2:

Default DataSource implementation is Hikari now instead of TomcatJDBC.

spring.datasource.url = jdbc...
spring.datasource.driver-class-name = com.mysql...

If you have provided above properties and still getting OP's 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

Add Hikari jdbc url property to use your datasource url property.

spring.datasource.hikari.jdbc-url = ${spring.datasource.url}

Checkout this answer also.

Upvotes: 1

Mohibur_Cou
Mohibur_Cou

Reputation: 75

Inside pom.xml file always keep the updated spring framework version. I had created a project with spring framework version 2.5.5 and it was working fine that time. After a couple of months, I found that it is not working correctly. Then I put the latest version of the spring framework. Then it works.

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>Updated version</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

Upvotes: 1

jonathasborges1
jonathasborges1

Reputation: 2816

So, I am had with similar problem, and this link helpfull

What I understood is that preset of project need to have a "RDBMS Database" and a "In Memory Database"

RDBMS Database

  • mysql
  • Postgres
  • oracle
  • SQL server

In Memory Database

  • H2 Database
  • HSQL Database
  • Derby Database

So, when I selected this preset, all worked greatenter image description here

Upvotes: 2

Hassan Tasleem
Hassan Tasleem

Reputation: 11

Its not a big issue just give Add in Application.properties: ``

spring.datasource.name=/name/

Upvotes: 0

saurabhshcs
saurabhshcs

Reputation: 927

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.

You need to configure the database driver and the JDBC connection properties to fix this 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.

application.properties

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

application.yaml

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

By Programming

@SpringBootApplication(exclude =  {DataSourceAutoConfiguration.class })

Upvotes: 74

CodingBee
CodingBee

Reputation: 1199

I faced the same issue in my code, adding this code in Application.java file helped me out-

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})

Upvotes: 7

Eric Ampire
Eric Ampire

Reputation: 309

I have added this annotation on the main class of my spring boot application and everything is working perfectly

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

Upvotes: 8

Apu Pradhan
Apu Pradhan

Reputation: 101

You need to configure the database driver and the JDBC connection properties to fix this 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.

application.properties

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

Upvotes: 2

Related Questions