Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 257
Asif Kamran Malick Avatar asked Dec 22 '17 09:12

Asif Kamran Malick


People also ask

How do you call DataSource in spring boot?

To access the Relational Database by using JdbcTemplate in Spring Boot application, we need to add the Spring Boot Starter JDBC dependency in our build configuration file. Then, if you @Autowired the JdbcTemplate class, Spring Boot automatically connects the Database and sets the Datasource for the JdbcTemplate object.

How do you fix failed to configure a DataSource URL attribute is not specified and no embedded DataSource could be configured?

Define the DataSource Using Properties. Since the issue occurs due to the missing database connection, we can solve the problem simply by providing the data source properties.

Where do you configure a DataSource in a spring boot application?

2.2. DataSource configuration is provided by configuration properties entries ( spring. datasource. * ) in application. properties file.


1 Answers

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.

like image 143
Janar Avatar answered Sep 30 '22 13:09

Janar