spring-guides / gs-accessing-data-mysql

Accessing data with MySQL :: Learn how to set up and manage user accounts on MySQL and how to configure Spring Boot to connect to it at runtime.
http://spring.io/guides/gs/accessing-data-mysql/
Apache License 2.0
201 stars 314 forks source link

Example fails to start with HikariDataSource related error #12

Closed sergiofgonzalez closed 6 years ago

sergiofgonzalez commented 6 years ago

Hi!

First of all, thank you very much for all the work you're putting in the examples and docs: It really makes the difference when learning new topics! 🥇

When running the example in the ./complete/ directory, with the only changed being done on my side is the update of the username/pass for the database i'm getting the following error: Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource

See the complete start log below:

2018-04-16 09:15:00.638  INFO 33768 --- [           main] hello.Application                        : Starting Application on MW7IF7J13IEI10 with PID 33768 (C:\Users\sergio.f.gonzalez\git\spring-examples-repo\complete\target\classes started by sergio.f.gonzalez in C:\Users\sergio.f.gonzalez\git\spring-examples-repo\complete)
2018-04-16 09:15:00.641  INFO 33768 --- [           main] hello.Application                        : No active profile set, falling back to default profiles: default
2018-04-16 09:15:00.678  INFO 33768 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@222114ba: startup date [Mon Apr 16 09:15:00 CEST 2018]; root of context hierarchy
2018-04-16 09:15:01.451  INFO 33768 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$e0ae4b3f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-04-16 09:15:01.675  INFO 33768 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-04-16 09:15:01.699  INFO 33768 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-16 09:15:01.700  INFO 33768 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-16 09:15:01.703  INFO 33768 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\dev-sw\jdk1.8.0_162\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_162/bin/server;C:/Program Files/Java/jre1.8.0_162/bin;C:/Program Files/Java/jre1.8.0_162/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\dev-sw\apache-maven-3.5.3\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\dev-sw\Heroku\bin;C:\Program Files (x86)\git\cmd;C:\dev-sw\Vagrant\bin;C:\Users\sergio.f.gonzalez\Downloads\~delete.me~\ec2-api-tools\ec2-api-tools-1.7.5.1\bin;C:\Program Files\OpenVPN\bin;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\GNU\GnuPG\pub;C:\Program Files\Amazon\AWSCLI\;C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\;C:\Users\sergio.f.gonzalez\AppData\Local\Microsoft\WindowsApps;C:\dev-sw\Git\cmd;;C:\dev-sw\spring-tool-suite\spring-tool-suite-3.9.2.RELEASE-e4.7.2-win32-x86_64\sts-bundle\sts-3.9.2.RELEASE;;.]
2018-04-16 09:15:01.797  INFO 33768 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-16 09:15:01.797  INFO 33768 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1120 ms
2018-04-16 09:15:01.907  INFO 33768 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-04-16 09:15:01.911  INFO 33768 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-16 09:15:01.912  INFO 33768 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-04-16 09:15:01.912  INFO 33768 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-04-16 09:15:01.912  INFO 33768 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-04-16 09:15:01.967 ERROR 33768 --- [           main] com.zaxxer.hikari.HikariConfig           : Failed to load driver class com.mysql.jdbc.Driver from HikariConfig class classloader sun.misc.Launcher$AppClassLoader@764c12b6
2018-04-16 09:15:01.970  WARN 33768 --- [           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.context.properties.bind.BindException: Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource
2018-04-16 09:15:01.972  INFO 33768 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-04-16 09:15:01.985  INFO 33768 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-04-16 09:15:01.989 ERROR 33768 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:

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

Action:

Update your application's configuration

If i downgrade the version of the spring-boot-starter-parent to 2.0.0.RELEASE the application works without problems.

Thank you very much!

Sergio

jpwilliams000 commented 6 years ago

Same

toannh0104 commented 6 years ago

Same problem

dsyer commented 6 years ago

The OP looks like a class path issue (bad jar file maybe). Did you clear the maven cache?

toannh0104 commented 6 years ago

I've cleaned my repository and download again via gradle & maven but the problem still happened

toannh0104 commented 6 years ago

The problem comes from the binary classpath so check again pom and gradle setting. It works fined, thank you @dsyer

dsyer commented 6 years ago

Is the database actually running? I don't see the same error as the OP if the database is unavailable, but the app won't start then either. Are we even talking about the same problem?

toannh0104 commented 6 years ago

The problem is MySQL binary is not in classpath yet. You can add a MySQL connection string into the application.properties file without add MySQL driver dependency to reproduce the issue. I'm using initial but I don't have that problem anymore

dsyer commented 6 years ago

MySQL is on the classpath via mysql:mysql-connector-java in the "complete" project. What do you mean? Is there actually an issue here? I guess not.

apexkid commented 6 years ago

I solved this issue by deleting the m2 folder of MVN and re-caching all dependencies.

balisbalis commented 5 years ago

MySQL is on the classpath via mysql:mysql-connector-java in the "complete" project. What do you mean? Is there actually an issue here? I guess not.

Adding

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

gives the error stated above if the version of spring-boot-starter-parent is 2.1.2.RELEASE but works fine when it is 2.0.0.RELEASE.

Doesn't it looks like an issue?

dsyer commented 5 years ago

Why would you add the JDBC starter? Anyway, it works for me, so no it doesn't look like an issue. Maybe you have a bad jar?

balisbalis commented 5 years ago

Why would you add the JDBC starter? Anyway, it works for me, so no it doesn't look like an issue. Maybe you have a bad jar?

I am just running on IDE. Because I need HikariDataSource class, I need to add jdbc thing.

dsyer commented 5 years ago

HikariDataSource is already included in the sample app (it is the default data source in Spring Boot 2). I would look at your Maven cache if I were you, to see if you can find the bad jar.

Parth94 commented 4 years ago

remove .driverClassName from data source builder instead add dependency of driver in pom also no need of spring-boot-starter-data-jdbc HikariDataSource will pick from there

robevansuk commented 4 years ago

seems it may be version dependent. Have had this issue for a little while. Decided to try a version bump and the issue has gone away. Now using:

implementation('com.zaxxer:HikariCP:3.4.3') implementation('org.springframework.data:spring-data-jdbc:1.1.2.RELEASE') implementation('mysql:mysql-connector-java:8.0.17')

Was previously using: implementation('com.zaxxer:HikariCP:3.3.1')

EDIT: This may be a problem with a dirty cache in intelliJ. I've since had a similar error and rebuilding the project within intelliJ resolved the issue: Build -> Rebuild Project

dsyer commented 4 years ago

If you use the code from the guide it comes with Hikari 3.4.1, Spring Data JDBC 2.2.3 and Mysql 8.0.18 (currently). So that should work too. You shouldn't need the explicit versions of any of those things (just Spring Boot).

sardorjumamuratov commented 4 years ago

I solved this problem by adding mysql connector dependency to my pom.xml file. lmao I forgot about connector.

abdelrhmanabdelhaie commented 4 years ago

use

mysql mysql-connector-java runtime
edine-noella commented 3 years ago

this issue for me was caused by the version of mysql. all i had to do is add the version of mysql in pom.xml dependencies (in my case the verion is 8.0.25)

`

mysql
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.25</version>

`

make sure to reload the maven dependecies before your run