11

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.

Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47
  • do you have mysql dependeny in your pom? – pvpkiran Dec 22 '17 at 09:24
  • Yes I can see teh dependency in the effective pom. Just fyi , I am using a spring-boot-starter-parent of version 2.0.0.M3 – Asif Kamran Malick Dec 22 '17 at 09:28
  • I believe driver class name should be specified like this `spring.datasource.driver-class-name=` – pvpkiran Dec 22 '17 at 09:30
  • I still get the error `Cannot load driver class: com.mysql.jdbc.Driver` – Asif Kamran Malick Dec 22 '17 at 09:35
  • can you share the mysql dependency you have in your pom – pvpkiran Dec 22 '17 at 09:40
  • The effective Pom : ` mysql mysql-connector-java 5.1.43 ` Apologies for the late response. – Asif Kamran Malick Dec 22 '17 at 10:43
  • 1
    Apparently you need a `DataSource` as you configure something that requires it. Add a proper datasource. You cannot add any arbitrary one (like you tried with the MySQL driver as that requires a running instance of MySQL and needs proper configuration). If you don't need it remove the dependencies that require a database (like JPA etc.) if you need one add a driver (like H2 for a in memory database) or the one matching the database you are connecting to. – M. Deinum Dec 22 '17 at 13:47

4 Answers4

11

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/

Atul
  • 1,560
  • 5
  • 30
  • 75
  • Thanks a lot Atul. I got this resolved by downgrading the `spring-boot-starter-parent` to `2.0.0.M3`. Earlier it was `2.0.0.M5` (I mistakenly quoted the version to be 2.0.0.M3 in my original post) Spring Boot versions seem so experimental. What's the point when I still have to take care of the versioning. I'm saving your suggestion for future reference. I have just starting learning Spring Boot. – Asif Kamran Malick Dec 23 '17 at 09:07
  • That is really strange. Because for datasource , you have to anyways add additional external dependency. May be spring boot version 2.0.0.M5 may not be playing well with datasource ? Also , You seem to be using spring boot 2 , can you try this with spring boot version 1.4 ? – Atul Dec 24 '17 at 05:18
  • I did include the h2 dependency also. Just didn't remember to take a note of which step actually resolved this. – Asif Kamran Malick Dec 24 '17 at 10:06
6

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.

Janar
  • 2,623
  • 1
  • 22
  • 32
  • I tried excluding `DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class` from the autoconfiguration and ran my app. It gives me this error: `java.lang.ClassCastException: org.springframework.boot.context.event.ApplicationReadyEvent cannot be cast to org.springframework.boot.web.context.WebServerInitializedEvent` Is that what you meant ? But I am not sure how do I remove **All** datasource autowirings. Sorry for the late response @Janar. Frequent powercut here. – Asif Kamran Malick Dec 22 '17 at 10:42
  • that is a completely different error and not related to the datasource (it probably means you fixed the problem with datasource), but it seems you have other errors in the code also – Janar Dec 22 '17 at 10:47
  • Thanks.This seems to be never ending loop of error.I don't like asking any moe questions as this seems to be wastage of your and other's time who looked into this(thanks to spring boot). I am just having a bad experience learning this spring boot. The Spring boot starter-parent never seems stable. Sometimes I see suggestions to downgrade, sometimes to upgrade.Seems quite contrary to Spring boot's mission itself. – Asif Kamran Malick Dec 22 '17 at 11:00
2

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

Zubair A.
  • 71
  • 3
0

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>
user1419261
  • 829
  • 8
  • 5