2
votes

I am creating an application using Spring Boot JPA, I am using MySQL as a database.

Following is my application.properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect

I have added following dependencies

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

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

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

When I checked in debug logs I can see mysql java connector in my classpath but still I am getting following errors

2019-07-29 10:03:00.742 INFO 10356 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-07-29 10:03:00.742 INFO 10356 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1534 ms 2019-07-29 10:03:00.789 WARN 10356 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': 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 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class 2019-07-29 10:03:00.789 INFO 10356 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2019-07-29 10:03:00.805 INFO 10356 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-07-29 10:03:00.805 ERROR 10356 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

*************************** 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).

6

6 Answers

4
votes

Spring Boot auto-configuration tries to configure the beans automatically based on the dependencies added to the classpath. Since you have the JPA dependency on your classpath, Spring Boot tries to automatically configure a JPA DataSource. The problem is, you haven’t given Spring the complete information it needs to perform the auto-configuration.

Add this missing property to your application.properties file, so that spring can autoconfigure

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Another way you can define your data source programmatically, by using the utility builder class DataSourceBuilder. For that you need to provide the database URL, username, password, and the SQL driver information to create your data source:

@Configuration
public class DatasourceConfig {
    @Bean
    public DataSource datasource() {
        return DataSourceBuilder.create()
                .driverClassName("com.mysql.cj.jdbc.Driver")
                .url("jdbc:mysql://localhost:3306/myDb")
                .username("root")
                .password("pass")
                .build();
    }
}
2
votes

I had the same problem, I resolved it by doing right click on the project, maven/update project, then you should select your project and accept.

1
votes

I had this problem too. My solution is : open module settings,and select resources,and right click it ,and select "Test Resources". Then it's solved.

setting page

0
votes

It was some mistake in my configuration which I couldn't detect, I re-created same project and things started working

0
votes

change:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb

to:

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/mydb
0
votes

Spring Autoconfigure looks for 2 properties to load the appropriate driver,

  1. spring.datasource.driverClassName (in this case the value would be 'com.mysql.cj.jdbc.Driver' from MySQL connector 4.4.1.3 onwards)
  2. spring.datasource.url (in this case it is jdbc:mysql://localhost:3306/mydb)

Actually Spring Autoconfigure just need spring.datasource.url and it can derive the driver class name from there.