Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you solve failed to determine a suitable driver class?

Error Message 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 does Spring Boot define DataSource?

Spring Boot provides a very good support to create a DataSource for Database. We need not write any extra code to create a DataSource in Spring Boot. Just adding the dependencies and doing the configuration details is enough to create a DataSource and connect the Database.

What is spring DataSource URL?

A DataSource is a factory for connections to the physical databases. It is an alternative to the DriverManager facility. A datasource uses a URL along with username/password credentials to establish the database connection. In Java, a datasource implements the javax.

Why do we use DataSource?

A DataSource object holds the credentials needed to get a connection to the database. Typically, a DataSource holds a user name recognized by the database server, a password for that user, and various settings to customize any future sessions with the database.


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

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

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


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.


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>