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

52

358

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

Map answered 7/7, 2018 at 9:29 Comment(1)
For those who encounter that - Sometimes we forgot to make maven reinstall (mvn clean install) after changing the pom.xml. So if you feel it's unexpected, try to do it too.Shipe
E
198

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
Eulogia answered 18/9, 2018 at 13:14 Comment(6)
@GhostDede please check in src/main/resources/application.propertiesAttwood
Thanks! It helped me to figure out what is going on my code.Pulpwood
If this driver: "com.mysql.jdbc.Driver" is deprecated, consider using this: "com.mysql.cj.jdbc.Driver" . It worked for me!Dorfman
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException:Libreville
as @saurabhshcs mentioned below you can also use org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration to stop the auto configuration. Then use a manual builder for the purpose using DataSourceBuilder.create(). Source: baeldung.com/spring-boot-failed-to-configure-data-sourceMonserratemonsieur
After doing this, it should have worked for me. Apparently, my issue was that the identation in the application.yaml file was incorrect and hence the entire datasource block was not identified. after aligning the identation properly, @Eulogia solution it began to work.Nonsmoker
A
343

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

Aliquot answered 1/10, 2019 at 11:21 Comment(11)
Works perfectly for when I know I need to use the DataSource at some point but don't need it immediately.Poff
It's really work but the reason for that error is adding database dependency to pom.xml and didn't add variable for connect to DB. // If you want to connect to DB you must add attribute in application.properties in the proper placeAnethole
Yeah, this is the solution I was looking for, as my application doesn't have any data hooks (just yet).Assorted
@SpringBootApplication(exclude = [DataSourceAutoConfiguration::class]) in KotlinSaville
this worked for me as I am using mongoDb. If you are using MySQL then pls go with the accepted answer, else please go with this one.Quartic
@TanakornLueangkajonvit What attribute should be added?Monograph
@TanakornLueangkajonvit , this link shows how to configure the application.properties baeldung.com/spring-boot-failed-to-configure-data-source.Foremast
spring boot seem quite trouble to guess what you want to do, why I cannot just add dependency in pom.xml but no need to use it?Goatfish
If I understand this correctly, this disables the automatic configuration of the datasource and we need to create such a bean in the application context, ourselves, right?Ewart
works perfectly for spring-boot-starter-data-mongodbGoebbels
also you must import it with import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;Trawl
E
198

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
Eulogia answered 18/9, 2018 at 13:14 Comment(6)
@GhostDede please check in src/main/resources/application.propertiesAttwood
Thanks! It helped me to figure out what is going on my code.Pulpwood
If this driver: "com.mysql.jdbc.Driver" is deprecated, consider using this: "com.mysql.cj.jdbc.Driver" . It worked for me!Dorfman
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException:Libreville
as @saurabhshcs mentioned below you can also use org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration to stop the auto configuration. Then use a manual builder for the purpose using DataSourceBuilder.create(). Source: baeldung.com/spring-boot-failed-to-configure-data-sourceMonserratemonsieur
After doing this, it should have worked for me. Apparently, my issue was that the identation in the application.yaml file was incorrect and hence the entire datasource block was not identified. after aligning the identation properly, @Eulogia solution it began to work.Nonsmoker
P
77

Your problem is the dependency of spring batch spring-boot-starter-batch that has a spring-boot-starter-jdbc transitive maven dependency.

Spring Batch is a framework for building reliable and fault tolerance enterprise batch jobs. It supports many features like restarting a failed batch, recording the status of the batch execution and so on. In order to achieve that Spring Batch uses a database schema to store the status of the registered jobs, the auto-configuration already provides you the basic configuration of the required data source and it is this configuration that requires the relational database configuration.

To solve this you must include some database driver like mysql, h2, etc. to configure the url.

Update: Just for getting start you can configure your application.yml like below:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:localhost;DB_CLOSE_ON_EXIT=FALSE
    username: admin
    password:

and of course in your pom.xml include the h2 dirver like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
       ....
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

....
    </dependencies>
...

</project>

The motivation, because you can not use mongo for this purpose, is that the usage of mongo is provided only for item readers and writers and not for managing the internal database of Spring Batch that is an internal schema, not a business schema. The query is plain SQL query and the internal abstraction relies on a relational database. It is necessary to have a database with ACID capability because every batch reads and writes a chunk of work and saves that information in order to restart the job. A NoSql solution is not suitable for this.

At the end you have configured a relational database in order to prepare Spring Batch for internal capability, the internal abstraction does not rely on mongo only on jdbc. Then mongo can be used but for the business side of the batch via item reader/writer.

I hope that this can help you to clear your doubts.

Pimento answered 7/7, 2018 at 9:42 Comment(4)
This is the my question - Still we need to use H2 if using MongoDB ? I used H2 but still the issue is same !!Map
I update the answer. I tried a simple getting start and with this configuration it works. I able to start my applicationPimento
you can exclude spring-jdbc from your spring-batch dependency or in the application class exclude the datasource bean loading @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})Saga
waste of time and illogical to install h2 in mongo project. this solution worked for me.. @ValerioVaudi thanks for the clarityBibcock
P
68

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 })
Predictor answered 22/11, 2020 at 1:8 Comment(3)
My problem was: In my yml setting, I put the spring.autoconfigure.exclude in the wrong place, such as spring.xxx.autoconfigure.exclude, making the setting path right solved my problem.Reflate
no wayy --it worked!Lyall
is there a way to re enable this configuration? im creating a database in the runtime and later I want to specify the jdbc url in order to the jpa execute their task en create the databse tables according tho the java class.Alyshaalysia
B
36

Not to the point of the question (can be related though), but, if you bootstrap a new project and wondering why do you get the same error, it may come from the artifactId of spring-boot-starter-data-jpa in the dependency section. I gave the dependency below. You will need to define the database to get rid of this.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Backbencher answered 26/2, 2019 at 19:26 Comment(2)
In my case it was spring-boot-starter-data-jdbc dependency that I commented in pom.xml and the problem went away! I guess it would be nice to include these dependencies only if you have the driver and other credentials ready which can then be updated in application.properties for the project to build successfully.Hiero
I encountered a similar issue described here. I was toying around with JPA as opposed to directly using Mongo, so I added the dependency above (via Intellij). I decided I didn't love the JPA way, so I commented it out of the pom.xml. That's when I must have started getting the error noted in the question at the top. I needed to run a mvn clean install and then Reload All Maven Projects from Intellij to get rid of the error.Hotfoot
S
31

Excluding the DataSourceAutoConfiguration.class worked for me:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
Steere answered 16/2, 2020 at 16:1 Comment(2)
best solution for non database based application.Diligent
Kotlin: @SpringBootApplication(exclude = [DataSourceAutoConfiguration::class])Saville
B
14

I had the same issue resolved by add <scope>provided</scope>

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

Source: https://github.com/spring-projects/spring-boot/issues/13796#issuecomment-413313346

Baryon answered 27/8, 2019 at 9:30 Comment(0)
H
13

This link helped.

Spring Boot auto-configuration tries to configure beans automatically based on the dependencies added to the classpath. And because we have a JPA dependency (spring-data-starter-jpa) on our classpath, it tries to configure it.

The problem: Spring boot doesn't have the all the info needed to configure the JPA data source i.e. the JDBC connection properties. Solutions:

  1. provide the JDBC connection properties (best)
  2. postpone supplying connection properties by excluding some AutoConfig classes (temporary - should be removed eventually)

The above link excludes the DataSourceAutoConfiguration.class with

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

But this didn't work for me. I instead, had to exclude 2 AutoConfig classes:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
Harte answered 15/9, 2019 at 13:29 Comment(5)
Worked for me but I still didnt get why I need to exclude it explicitlyBlackshear
hey @SaurabhVerma, I've edited the answer to explain the rationale of excluding the AutoConfig classes.Harte
Excluding only DataSourceAutoConfiguration worked for me on 2.2.1.RELEASE of spring-boot-starter-parent.Driskill
Caused by: java.lang.IllegalArgumentException: JPA metamodel must not be empty!Libreville
If you need to exclude data source, the application is loading dependencies that are not needed. I would rather exclude unwanted artifact in pom. maven.apache.org/guides/introduction/…Hispid
E
13

It can be that your resources directory is not added to classpath when creating a project via Spring Initializr. So your application is never loading the application.properties file that you have configured.

To make a quick test if this is the case, add the following to your application.properties file:

server.port=8081

Now when running your application you should see in the spring boot console output something like this:

INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): **8081** (http) with context path ''

If your port is still default 8080 and not changed to 8081, your application.properties files is obviously not loading.

You can also check if your application runs with gradle bootRun from command line. Which most likely will be work.

Solution:

  1. Close IntelliJ, then inside your project folder delete the ".idea" folder
  2. Reimport your project to IntelliJ like following: "Import Project" -> "select ONLY your build.gradle file to import". (IntelliJ will automatically grab the rest)
  3. build and run your application again

See official answer by IntelliJ Support: IDEA-221673

Emelinaemeline answered 31/1, 2020 at 13:35 Comment(3)
For me it was enough to right click on the Resources folder, and choose "Mark directory as" -> "Resources Root".Arawn
I tried this, and Tomcat correctly starts on 8081 (so the file is being recognized) but for some reason, excluding the DataSourceAutoConfig makes my spring boot app 'hang' on the initialisation. I tried the annotation, same result. No error as before, just 'freeze'.. :/Assertive
this works for me, i change port from server.port=8081 to server.port=8082Chittagong
P
10

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

Periphrasis answered 21/5, 2021 at 21:47 Comment(0)
H
8

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

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
Hemoglobin answered 22/12, 2019 at 12:7 Comment(0)
V
6

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

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

Valentinavalentine answered 12/4, 2019 at 17:1 Comment(3)
Can you expand on this answer please?Degauss
I would rather explain the answer here.Backbencher
If using any other method for configuring data connection, use the following exclusion in application.properties : spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationVacuum
R
6

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

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

Rodge answered 5/4, 2021 at 7:19 Comment(1)
Yes. This could be the real solution when the Spring boot batch doesn't requires Data source, We can exclude them.Diplopia
S
4

For spring boot version 2.X.X below configuration worked for me.

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

Old jdbc driver is deprecated. The new one is mentioned on above configuration. Please use the same and restart the project.

Sicken answered 21/4, 2019 at 11:21 Comment(0)
S
3

It can be because you have jpa dependendencies and plugins...

just comment it if not use(build.gradle or pom file)

e. g.

// kotlin("plugin.jpa") version "1.3.61"

// implementation("org.springframework.boot:spring-boot-starter-data-jpa")
Spireme answered 23/3, 2020 at 11:23 Comment(1)
implementation("org.springframework.boot:spring-boot-starter-data-r2dbc") as wellSaville
L
3

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

Lettyletup answered 7/6, 2022 at 18:4 Comment(1)
Looks like he use MongoDB rather than mysql.Orthochromatic
S
2

It's happening because the @valerio-vaudi said.

Your problem is the dependency of spring batch spring-boot-starter-batch that has a spring-boot-starter-jdbc transitive maven dependency.

But you can resolve it set the primary datasource with your configuration

 @Primary
 @Bean(name = "dataSource")
 @ConfigurationProperties(prefix = "spring.datasource")
 public DataSource getDataSource() {
      return DataSourceBuilder.create().build();
 }

 @Bean
 public JdbcTemplate jdbcTemplate(DataSource dataSource) {
      return new JdbcTemplate(dataSource);
 }
Speak answered 23/10, 2019 at 0:31 Comment(0)
A
2

If you're using Gradle, rebuild Gradle can solve this problem.

Antivenin answered 13/6, 2020 at 9:59 Comment(1)
Wow, this solved it for me. And I keep forgetting the solution.Flotation
C
2

If you have a JPA dependency in your pom.xml then just remove it. This solution worked for me.

Celia answered 4/7, 2020 at 6:7 Comment(0)
I
2

Add this annotation in main java file

@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
Inamorato answered 10/8, 2020 at 11:41 Comment(0)
S
2

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

Sharpe answered 24/10, 2021 at 3:39 Comment(0)
N
2

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

Nutrilite answered 3/6, 2023 at 10:53 Comment(0)
G
1

It simply means you have downloaded a spring starter code with database dependency without configuring your database, So it doesn't know how to connect. For Spring boot version 2.18 do the following steps to fix it.

  1. Create a database for the driver you have downloaded ie mysql/mongo etc.

  2. In your applications.properties file add the db connection info. Sample is given for mysql if your db is mongo change it for mongo.

spring.datasource.url=jdbc:mysql://localhost:3306/db_name_that_you_created
spring.datasource.username=your_db_username_here
spring.datasource.password=your_db_pass_here
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
  1. Reboot the server it will be running.
Gaelic answered 30/9, 2019 at 6:20 Comment(0)
D
1

If datasource is defined in application.resources, make sure it is locate right under src/main and add it to the build path.

Drunkometer answered 15/11, 2019 at 3:33 Comment(0)
B
1

I had the same issue and tried all suggestions above, but didnt work out. I am posting my answer for furture readers. Before it was working fine but somehow it apeared again. I resolved this issue by removing some unnecessary plugnins and depencies from pom.xml

  1. First of all, I changed default packaging type to jar (Spring Boot Initializer gives pom in packaging)

    <packaging>jar</packaging>

  2. I added unintentional some plugins:

    <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <attachClasses>true</attachClasses> <webXml>target/web.xml</webXml> <webResources> <resource> <directory>src/main/webapp</directory> <filtering>true</filtering> </resource> </webResources> </configuration> </plugin>

I hope my answer will help someone.

Baulk answered 28/12, 2019 at 14:30 Comment(0)
R
1

If you added "spring-boot-starter-data-jpa" dependency in pom.xml, Please add respective database in dependency like h2 and etc.

React answered 11/8, 2020 at 6:46 Comment(0)
I
1

For me the resource folder was getting excluded on a maven update/build. I went to Build Path>Source and found that src/main/resources have "Excluded **". I removed that entry (Clicked on Excluded **>Remove>Apply and Close).

Then it worked fine.

enter image description here

Implantation answered 2/9, 2020 at 7:12 Comment(0)
G
1

If you are using YAML for configuration, then it might be indentation problem. Thoroughly check the YAML files.

Garlan answered 26/12, 2020 at 19:14 Comment(0)
C
1

Check your application.properties file. One of the probable reason for it is that

  • you might be added "Spring Data" maven plugin and you are not providing the datastore details in application.properties file.
Cheryle answered 9/2, 2021 at 5:2 Comment(0)
S
1

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
Scum answered 6/3, 2021 at 21:21 Comment(0)
L
1

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>
Letourneau answered 31/12, 2021 at 17:50 Comment(0)
E
1

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.

Entablature answered 24/1, 2022 at 12:46 Comment(0)
K
1

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

Kiloliter answered 8/2, 2022 at 5:27 Comment(0)
I
1

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
Integration answered 25/1, 2023 at 9:17 Comment(0)
P
0

This one worked for me, for MySQL: (Application properties)

spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&
useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
Panacea answered 7/1, 2020 at 12:41 Comment(0)
S
0

I removed an obsolete dependency on mybatis in the pom.xml to get mine running.

Soursop answered 19/2, 2020 at 14:17 Comment(0)
I
0

I meet same error when start a new project. Use command line works for me.

./gradlew bootRun
Inspect answered 27/9, 2020 at 16:36 Comment(0)
K
0

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

spring.datasource.name=/name/

Kktp answered 4/10, 2021 at 12:18 Comment(0)
D
0

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à

Dyna answered 29/3, 2022 at 5:34 Comment(0)
F
0

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.

Forth answered 10/5, 2022 at 15:23 Comment(0)
A
0

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

Aeolian answered 12/11, 2022 at 6:56 Comment(0)
S
0

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

Sialkot answered 22/12, 2022 at 20:25 Comment(0)
C
0

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

Cobaltite answered 17/4, 2023 at 6:12 Comment(0)
I
0

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'"
Impressure answered 17/5, 2023 at 19:9 Comment(0)
D
0

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

Damek answered 28/7, 2023 at 16:58 Comment(0)
A
0

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.

Annular answered 3/8, 2023 at 5:31 Comment(0)
M
0

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!

Mathers answered 5/9, 2023 at 11:3 Comment(0)
C
0

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>
Colorant answered 19/10, 2023 at 14:15 Comment(0)
N
0

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.

Noctambulism answered 8/3 at 6:29 Comment(0)
S
0

executing mvn clean command worked in my case.

Stupefacient answered 16/3 at 12:19 Comment(0)
F
-2

I think when importing modules you have imported another package, Go to modules and remove al of them. After that import modules from the package of the project

Firedrake answered 21/5, 2020 at 13:22 Comment(0)
C
-3

Adding h2 dependency to the pom file can resolve such issues. ...... com.h2database h2 ......

Cuisine answered 25/3, 2019 at 21:43 Comment(1)
Covered by existing answerMckown

© 2022 - 2024 — McMap. All rights reserved.