32

I am trying to learn microservices by creating a dummy project. I have a config repository in git and a config server running at port 8888.It's working properly as I can see my settings as below: enter image description here

Now I have two microservice projects 1) customer service and 2) customer-account-service. customer microservice is running properly while customer-account-microservice is unable to start and throwing below exception:

*Caused by: 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: org.h2.Driver
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE]
    ... 66 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver
    at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.0.11.RELEASE.jar:5.0.11.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:224) ~[spring-boot-autoconfigure-2.0.7.RELEASE.jar:2.0.7.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:176) ~[spring-boot-autoconfigure-2.0.7.RELEASE.jar:2.0.7.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:43) ~[spring-boot-autoconfigure-2.0.7.RELEASE.jar:2.0.7.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:83) ~[spring-boot-autoconfigure-2.0.7.RELEASE.jar:2.0.7.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.11.RELEASE.jar:5.0.11.RELEASE]
    ... 67 common frames omitted*

bootstrap.properties configuration for customer-account-microservice is as below:

enter image description here

pom.xml configuration for customer-account-microservice is as below:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.microservice</groupId>
    <artifactId>customer-account-microservice</artifactId>
    <version>1</version>
    <name>customer-account-microservice</name>
    <description>Spring starter project for demonstrating spring feign client and circuit breaker</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-services.version>2.0.3.RELEASE</spring-cloud-services.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.pivotal.spring.cloud</groupId>
            <artifactId>spring-cloud-services-starter-circuit-breaker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>io.pivotal.spring.cloud</groupId>
                <artifactId>spring-cloud-services-dependencies</artifactId>
                <version>${spring-cloud-services.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Please help me in fixing the above exception.

user2800089
  • 2,015
  • 6
  • 26
  • 47
  • 2
    May I ask why the h2 dependency is part the the `dependencyManagement` block instead the dependencies block? – sn42 Jan 17 '19 at 07:05
  • 1
    @sn42 copy paste error and after moving h2 dependency under dependencies block, code worked. Thanks alot for pointing this error. – user2800089 Jan 17 '19 at 07:14

12 Answers12

49

It seems that your dependency

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

has to be in dependencies element, not in dependencyManagement. Just look at this. Or maybe you have a child artifact, in that case you must share another pom xml.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Artur Vakhrameev
  • 794
  • 7
  • 11
15

in my case, I had a space after the "driver"

spring.datasource.driverClassName=org.h2.Driver...

I deleted it and it works just fine

Anass
  • 151
  • 1
  • 2
14

I change it to 'compile' and it solved my problem.

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
            <scope>compile</scope>
        </dependency>
Yanping Liu
  • 141
  • 1
  • 2
9

I was facing the same issue in IDEA IntellJ even after using the correct dependency in pom.xml. It boils down to IDE specific issue where it was not able to download h2 dependency. It worked after restarting the IDE.

Ashish Lahoti
  • 648
  • 6
  • 8
7

In my case the problem was caused by some trailing blank characters behind the driver classname in application.properties

This fails (dots in the code snippet below should be read as whitespace chars):

spring.datasource.driverClassName=org.h2.Driver...

This succeeds:

spring.datasource.driverClassName=org.h2.Driver
3

I commented spring.datasource.driverClassName=org.h2.Driver, that worked like a charm for me

HBD31
  • 31
  • 2
2

In my case I gave the gradle dependency wrong.

I should give it in dependencies {implementation}, but I gave it in dependencies {testImplementation} in build.gradle file

The below mentioned is right

dependencies {
  implementation 'com.h2database:h2:2.1.212' 
}
ddc
  • 885
  • 6
  • 12
1

I had this problem because I was using an outdated version of the h2 dependency :

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.193</version>
    </dependency>

I fixed it by removing the version tag so that the latest version is downloaded :

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
Ruthless
  • 132
  • 3
  • 15
1

In my case, In application.properties file, there are white spaces after adding configurations You can see IDEA IntellJ like this enter image description here

Should remove all the white spaces in application.properties file. Then the test cases failed issue will be solved

0

I hope you have added dependencies now add spring.datasource.platform=h2 in your application.properties

it worked for me

0

These are the changes I made:

application-test.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=
spring.datasource.username=
spring.datasource.password=

unsetting those datasource properties was only needed because I set them in the base application.properties. It'd be simpler just to not define them there and instead have them in the profile-specific properties, but leaving this here in case anyone else wants a quick/lazy fix

build.gradle.kts

testImplementation("com.h2database:h2")
mowwwalker
  • 16,634
  • 25
  • 104
  • 157
-1
<dependency>
 <groupId>com.h2database</groupId>
 <artifactId>h2</artifactId>
 <scope>test</scope>
</dependency>

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
</dependency>

Repleace scope test to runtime .it worked for me!.

  • 1
    if you do this and leave the dependencyManagement section of the pom.xml as is the case w/ the pom.xml that is submitted w/ the question, it won't work. – Mag Musik May 13 '23 at 07:07