java - Spring Boot - DataSource Runtime Error -
i've been stuck on implementing mysql using maven , springboot in eclipse. trying run application keeps showing me errors.
2017-07-28 11:49:06.767 info 5296 --- [ main] io.msela.employeeapidataapplication : starting employeeapidataapplication on ctse-msela pid 5296 (c:\users\msela\documents\workspace-sts-3.9.0.release\myspringdataemployeeproject\target\classes started msela in c:\users\msela\documents\workspace-sts-3.9.0.release\myspringdataemployeeproject) 2017-07-28 11:49:06.767 info 5296 --- [ main] io.msela.employeeapidataapplication : no active profile set, falling default profiles: default 2017-07-28 11:49:06.830 info 5296 --- [ main] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@4718df: startup date [fri jul 28 11:49:06 cest 2017]; root of context hierarchy 2017-07-28 11:49:07.970 info 5296 --- [ main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat initialized port(s): 8080 (http) 2017-07-28 11:49:07.983 info 5296 --- [ main] o.apache.catalina.core.standardservice : starting service [tomcat] 2017-07-28 11:49:07.983 info 5296 --- [ main] org.apache.catalina.core.standardengine : starting servlet engine: apache tomcat/8.5.15 2017-07-28 11:49:08.077 info 5296 --- [ost-startstop-1] o.a.c.c.c.[tomcat].[localhost].[/] : initializing spring embedded webapplicationcontext 2017-07-28 11:49:08.077 info 5296 --- [ost-startstop-1] o.s.web.context.contextloader : root webapplicationcontext: initialization completed in 1247 ms 2017-07-28 11:49:08.202 info 5296 --- [ost-startstop-1] o.s.b.w.servlet.servletregistrationbean : mapping servlet: 'dispatcherservlet' [/] 2017-07-28 11:49:08.202 info 5296 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'characterencodingfilter' to: [/*] 2017-07-28 11:49:08.202 info 5296 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'hiddenhttpmethodfilter' to: [/*] 2017-07-28 11:49:08.202 info 5296 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'httpputformcontentfilter' to: [/*] 2017-07-28 11:49:08.202 info 5296 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'requestcontextfilter' to: [/*] 2017-07-28 11:49:08.233 warn 5296 --- [ main] ationconfigembeddedwebapplicationcontext : exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'org.springframework.boot.autoconfigure.orm.jpa.hibernatejpaautoconfiguration': unsatisfied dependency expressed through constructor parameter 0; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'datasource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/datasourceconfiguration$tomcat.class]: bean instantiation via factory method failed; nested exception org.springframework.beans.beaninstantiationexception: failed instantiate [org.apache.tomcat.jdbc.pool.datasource]: factory method 'datasource' threw exception; nested exception java.lang.illegalstateexception: cannot load driver class: com.mysql.jdbc.driver 2017-07-28 11:49:08.233 info 5296 --- [ main] o.apache.catalina.core.standardservice : stopping service [tomcat] 2017-07-28 11:49:08.249 info 5296 --- [ main] utoconfigurationreportlogginginitializer : error starting applicationcontext. display auto-configuration report re-run application 'debug' enabled. 2017-07-28 11:49:08.265 error 5296 --- [ main] o.s.boot.springapplication : application startup failed org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'org.springframework.boot.autoconfigure.orm.jpa.hibernatejpaautoconfiguration': unsatisfied dependency expressed through constructor parameter 0; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'datasource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/datasourceconfiguration$tomcat.class]: bean instantiation via factory method failed; nested exception org.springframework.beans.beaninstantiationexception: failed instantiate [org.apache.tomcat.jdbc.pool.datasource]: factory method 'datasource' threw exception; nested exception java.lang.illegalstateexception: cannot load driver class: com.mysql.jdbc.driver @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:749) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.constructorresolver.autowireconstructor(constructorresolver.java:189) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.autowireconstructor(abstractautowirecapablebeanfactory.java:1193) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1095) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:513) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymethod(constructorresolver.java:372) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiateusingfactorymethod(abstractautowirecapablebeanfactory.java:1173) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1067) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:513) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.context.support.abstractapplicationcontext.getbean(abstractapplicationcontext.java:1078) ~[spring-context-4.3.9.release.jar:4.3.9.release] @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:857) ~[spring-context-4.3.9.release.jar:4.3.9.release] @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:543) ~[spring-context-4.3.9.release.jar:4.3.9.release] @ org.springframework.boot.context.embedded.embeddedwebapplicationcontext.refresh(embeddedwebapplicationcontext.java:122) ~[spring-boot-1.5.4.release.jar:1.5.4.release] @ org.springframework.boot.springapplication.refresh(springapplication.java:693) [spring-boot-1.5.4.release.jar:1.5.4.release] @ org.springframework.boot.springapplication.refreshcontext(springapplication.java:360) [spring-boot-1.5.4.release.jar:1.5.4.release] @ org.springframework.boot.springapplication.run(springapplication.java:303) [spring-boot-1.5.4.release.jar:1.5.4.release] @ org.springframework.boot.springapplication.run(springapplication.java:1118) [spring-boot-1.5.4.release.jar:1.5.4.release] @ org.springframework.boot.springapplication.run(springapplication.java:1107) [spring-boot-1.5.4.release.jar:1.5.4.release] @ io.msela.employeeapidataapplication.main(employeeapidataapplication.java:16) [classes/:na] caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'datasource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/datasourceconfiguration$tomcat.class]: bean instantiation via factory method failed; nested exception org.springframework.beans.beaninstantiationexception: failed instantiate [org.apache.tomcat.jdbc.pool.datasource]: factory method 'datasource' threw exception; nested exception java.lang.illegalstateexception: cannot load driver class: com.mysql.jdbc.driver @ org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymethod(constructorresolver.java:599) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiateusingfactorymethod(abstractautowirecapablebeanfactory.java:1173) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1067) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:513) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:202) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.config.dependencydescriptor.resolvecandidate(dependencydescriptor.java:208) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1138) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1066) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.constructorresolver.resolveautowiredargument(constructorresolver.java:835) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:741) ~[spring-beans-4.3.9.release.jar:4.3.9.release] ... 28 common frames omitted caused by: org.springframework.beans.beaninstantiationexception: failed instantiate [org.apache.tomcat.jdbc.pool.datasource]: factory method 'datasource' threw exception; nested exception java.lang.illegalstateexception: cannot load driver class: com.mysql.jdbc.driver @ org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy.java:189) ~[spring-beans-4.3.9.release.jar:4.3.9.release] @ org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymethod(constructorresolver.java:588) ~[spring-beans-4.3.9.release.jar:4.3.9.release] ... 41 common frames omitted caused by: java.lang.illegalstateexception: cannot load driver class: com.mysql.jdbc.driver @ org.springframework.util.assert.state(assert.java:70) ~[spring-core-4.3.9.release.jar:4.3.9.release] @ org.springframework.boot.autoconfigure.jdbc.datasourceproperties.determinedriverclassname(datasourceproperties.java:231) ~[spring-boot-autoconfigure-1.5.4.release.jar:1.5.4.release] @ org.springframework.boot.autoconfigure.jdbc.datasourceproperties.initializedatasourcebuilder(datasourceproperties.java:183) ~[spring-boot-autoconfigure-1.5.4.release.jar:1.5.4.release] @ org.springframework.boot.autoconfigure.jdbc.datasourceconfiguration.createdatasource(datasourceconfiguration.java:42) ~[spring-boot-autoconfigure-1.5.4.release.jar:1.5.4.release] @ org.springframework.boot.autoconfigure.jdbc.datasourceconfiguration$tomcat.datasource(datasourceconfiguration.java:56) ~[spring-boot-autoconfigure-1.5.4.release.jar:1.5.4.release] @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.8.0_131] @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_131] @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_131] @ java.lang.reflect.method.invoke(unknown source) ~[na:1.8.0_131] @ org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy.java:162) ~[spring-beans-4.3.9.release.jar:4.3.9.release] ... 42 common frames omitted
here's xml:
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>io.msela</groupid> <artifactid>course-api-data</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>course-api-data</name> <description>course api spring data</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.5.4.release</version> <relativepath/> <!-- lookup parent repository --> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
and here app:
package io.msela; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.autoconfigure.domain.entityscan; import org.springframework.context.annotation.componentscan; import org.springframework.data.jpa.repository.config.enablejparepositories; import io.msela.springbootstarter.employee.employeerepository; @springbootapplication @componentscan({"io.msela"}) @entityscan("io.msela.springbootstarter") @enablejparepositories(basepackageclasses = employeerepository.class) public class employeeapidataapplication { public static void main(string[] args) { springapplication.run(employeeapidataapplication.class, args); } }
and properties file:
spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.database-platform = org.hibernate.dialect.mysqldialect spring.datasource.url=jdbc:mysql://localhost:3306/db_example spring.datasource.username=springuser spring.datasource.password=thepassword spring.datasource.driver-class-name=com.mysql.jdbc.driver
it seems poor thing can't initialize datasource. made project work using internal database, mysql seems different beast.
you don't need specify :
spring.datasource.driver-class-name=com.mysql.jdbc.driver
declaring :
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
will allow spring boot discover correct driver class , load it.
the spring boot documentation states :
you won’t need specify driver-class-name since spring boot can deduce databases url.
besides, says :
i.e. if set spring.datasource.driver-class-name=com.mysql.jdbc.driver class has loadable.
but thrown exception shows class not loadable :
caused by: java.lang.illegalstateexception: cannot load driver class: com.mysql.jdbc.driver
so, suppose have classloading issue.
here way spring boot works assert driver class loadable :
public class datasourceproperties { ... public string determinedriverclassname() { if (stringutils.hastext(this.driverclassname)) { assert.state(driverclassisloadable(), "cannot load driver class: " + this.driverclassname); return this.driverclassname; } ...
that invokes :
... private boolean driverclassisloadable() { try { classutils.forname(this.driverclassname, null); return true; } catch (unsupportedclassversionerror ex) { // driver library has been compiled later jdk, propagate error throw ex; } catch (throwable ex) { return false; } } ... }
in case, enter in catch
statement :
catch (throwable ex) { return false; }
so classutils.forname(this.driverclassname, null);
doesn't manage load class , throws exception.
problem root exception caught in catch
shocked spring boot.
error message general.
if interested in understanding root cause or you using rare dbms not automatically mapped spring boot driver class, run in debug mode , set breakpoint @ statement :
classutils.forname(this.driverclassname, null);
Comments
Post a Comment