Asif Kamran Malick
Asif Kamran Malick

Reputation: 3251

DataSource Error while running Spring Boot application

I am newbie in Spring boot.I get this error

Cannot determine embedded database driver class for database type NONE

whenever trying to run my spring-boot start web app(I am trying to test the actuator and hal browser). Over the last eight hours or so I have tryied several suggestions over google/stackoverflow. But doesn't seem to work for me. I still keep getting another error.

First try: I followed both the methods mentioned in journaldev

If I use the first method i.e. annotating my main application class with @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }), I get this error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

If I use the second method which, I still get another error:

Binding to target [Bindable@7c551ad4 type = com.zaxxer.hikari.HikariDataSource, value = 'provided', annotations = array<Annotation>[[empty]]] failed:

    Property: driverclassname
    Value: com.mysql.jdbc.Driver
    Origin: "driverClassName" from property source "source"
    Reason: Unable to set value for property driver-class-name

I also tried Andy Wilkinson's suggestion and added

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/mydb

to my application.properties file but I got this error :

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver

I also tried with providing the username and pwd(not sure if that's required as I am not trying to access my database), but didn't work for me. If it's reqwuired I can provide my pom configurations too.

Upvotes: 12

Views: 62138

Answers (5)

Ankur Pandey
Ankur Pandey

Reputation: 51

For me, the issue is resolved after adding the below dependency in pom.xml.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>

Upvotes: 0

user1419261
user1419261

Reputation: 944

make sure the mysql dependency s there in the pom.xml and comment the h2 dependency

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

Upvotes: 0

Zubair A.
Zubair A.

Reputation: 89

You need to add JDBC DRIVER dependency in pom file, and then should work

Upvotes: 3

Atul
Atul

Reputation: 1590

Below configuration is working perfectly fine for me -

application.properties -

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/rolb
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.initialize=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

pom.xml -

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

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

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

         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

If you want , you can also download source for my example application to compare - https://github.com/atulajoshi24/springboot-rest.git

The related blog post for the same - http://thejavatechie.com/2017/12/21/single-page-application-using-spring-boot-rest-and-angular-1-part-1/

Upvotes: 11

Janar
Janar

Reputation: 2701

You said you don't need to access database so you should be able to use

@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })

and removing all autowirings which include datasource. The exception you got

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

says that you are trying to autowire datasource somewhere but you don't have one configured (since you excluded it). Just remove the autowired datasource and it should work.

If you do need to use database, then there seems to be a problem with mysql driver - make sure you have one added as dependency.

Upvotes: 6

Related Questions