Using H2 Database with Spring and Gradle

In-memory Database for Testing

Posted by jyaquinas on March 01, 2022 · 1 min read

What is H2 Database?

H2 Database is a in-memory database that is typically used for testing.

Setup

Add the h2database dependency in your ‘build.gradle’ file.

dependencies {
    implementation 'com.h2database:h2'
}

If you don’t have an ‘application.properties’ file under src -> main -> resources, add one.

Add the following lines:

spring.h2.console.enabled=true

This will let you access the DB through the console. You can access it through /h2-console, like:
localhost:8080/h2-console

spring.datasource.url=jdbc:h2:mem:testdb

This specifies the JDBC url you need to use for connecting to the DB. If this is not set, it will be automatically generated for you. You can then find the url in the console output, something like this:

2022-02-27 21:02:53.192  INFO 5544 --- [           main] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:867794d1-6dd5-4b6e-a1f1-afeb1368f399'

The default username will be sa with no password.