janssen-dev
janssen-dev

Reputation: 2771

Spring H2 database EmbeddedDatabaseFactory wrong connection URL

I'm trying to build a simple REST-Backend with Spring-Boot 2, Java 11, H2 database and Gradle. I want to use an embedded (file) H2 database, so my (relevant) application.yml (I know it is read in correctly because other values work) looks like this:

spring:
    datasource:
        url: "jdbc:h2:./customdb"
        username: sa
        driver-class-name: org.h2.Driver
    jpa:
        database-platform: org.hibernate.dialect.H2Dialect
        show-sql: false
        hibernate:
            ddl-auto: update

My build.gradle:

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'net.impfox'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    compile 'org.springframework.boot:spring-boot-starter-data-rest:2.1.3.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
    compile 'javax.persistence:javax.persistence-api:2.2'
    compile 'org.hibernate:hibernate-core'
    compile 'org.springframework.data:spring-data-jpa'
}

However, when I start the application, there is no file created and the log says:

...EmbeddedDatabaseFactory: Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'

I'm not quite sure why it says Starting embedded database because as you can see in the url, it actually starts an in-memory database called testdb. So why is it using jdbc:h2:mem:testdb as JDBC url and not my configured jdbc:h2:./customdb?

Upvotes: 1

Views: 1856

Answers (1)

janssen-dev
janssen-dev

Reputation: 2771

Check your gradle dependencies!

Instead of
compile 'org.springframework.data:spring-data-jpa',
use the spring-boot-starter-data-jpa, so something like
compile 'org.springframework.boot:spring-boot-starter-data-jpa:2.1.4.RELEASE'.
Also make sure gradle is actually applying this change.

The problem is that spring-data-jpa doesn't include all the functionality needed. The spring-boot-starter-data-jpa depends on the spring-data-jpa, but also on some other artifacts.

enter image description here

Upvotes: 1

Related Questions