Integrate H2 In-memory Database with Spring Boot

Spring Boot and H2 Database

This tutorial will show you how to integrate in-memory H2 database with Spring Boot and build using Gradle/Maven tools. You will see here simple Spring Boot Data JPA example with Spring REST API to understand how it works.

H2 is in-memory database, so it does not have persistence capability once your Spring Boot application is closed. But you can check data in H2 database by accessing the console in the browser as long as your Spring Boot application is running.

What is an in-memory database?

An in memory database is created when an application starts up and destroyed when the application is stopped.

Using in memory database has lots of advantages such as:

  • No need to setup the database
  • Almost zero configuration
  • Almost zero maintenance
  • It is very easy to use for learning, proof of concepts and unit testing

H2 is one of the popular in memory database and Spring Boot provides very easy configuration for an in memory database like H2.

Why is in memory database required?

Let’s consider a cenario when you want to do a quick proof of concept(POC) and using a traditional database involves a lot of overhead.

Another scenario, on your unit tests:

  • You don’t want them to fail when some data/schema get changed in the database.
  • Multiple developers might be running the tests in parallel.

Remember you should not replace the more robust database servers, such as, MySQL, Oracle, PostgreSQL, etc. by H2 database for your actual applications.


Spring Boot 2.1 – 2.5.4/3.1.5, H2 1.3.176 – 1.4.200/1.4.214, Gradle 4.x/5.x, Maven 3.8.2/3.8.5, Java 1.8/19

Project Setup

Create a gradle or maven based project in your favorite IDE or tool.

If you are using maven based project then you can use the following pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=""








If you are using gradle based project then you can use the following build.gradle script.

buildscript {
	ext {
		springBootVersion = '2.1.3.RELEASE'
    repositories {
    dependencies {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
dependencies {

As you see in the above build script I have just included the required dependencies for Spring Data JPA, H2 in memory database and Web module to perform REST operations with H2 database.

Entity Class

Create below entity class in order to define employee object.

package com.roytuts.spring.boot.h2.integration.entity;

// @Table(name = "employee")
public class Employee {

	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	private String name;

	@Column(name = "email_address")
	private String email;

	public Integer getId() {
		return id;

	public void setId(Integer id) { = id;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getEmail() {
		return email;

	public void setEmail(String email) { = email;


In the above class, I have annotated the Employee class with @Entity to map the Employee object with H2 database table employee. Table name is optional and Hibernate will create the same table name as the Entity class name.

I have denoted primary column with @Id and I am also generating the auto-increment values for primary column using @GeneratedValue annotation with generation type specification. Generation strategy is optional and Hibernate will automatically generate sequence for the id field in the table.

If your column name and Java class’s attribute name is same then you don’t need to specify the corresponding table column name @Column annotation. For example, in the above Employee class the corresponding table will have same name with id and name but email as a Java attribute, whereas the corresponding column name in table will be email_address.

You may not need to put any database configuration into file as I will use the default database configuration for H2 database. The database configuration for H2 is set by Spring Boot given below.


But here I am going to access the H2 database console using the browser to perform query on the database table, so I will override the above default configurations so that I can see the similar kind of database operations as we se for MySQL, PostgreSQL, etc.

I am going to use the following configurations for H2 database in this Spring Boot application:

spring.datasource.password= = true

spring.h2.console.enabled = true

spring.jpa.hibernate.ddl-auto = create


In the above configuration, in the datasource URL, notice that I have put mem because it is an in-memory database.

The default username is sa and there is no need to input password for connecting to the database. = true tells to log the executed query in the console.

spring.h2.console.enabled = true tells to enable the H2 console so that you can access it through browser. If this value is false you won’t be able to access the H2 console.

spring.jpa.hibernate.ddl-auto = create tells to create the table in the in-memory roytuts database.

spring.h2.console.path=/h2console/ tells that your H2 database console is accessible at path /h2console. So if your Spring Boot application is running at port 8080 then your H2 console is accessible at http://localhost:8080/h2console. If you are using Spring Security then make sure to exclude this path (/h2console) from your security configuration.


The data.sql file is required in order to insert some initial data to show employee data to the end users when they call the REST service.

This file is put under classpath directory src/main/resources.

insert into employee(name,email_address)
insert into employee(name,email_address)
insert into employee(name,email_address)
insert into employee(name,email_address)

spring.jpa.defer-datasource-initialization=true in the file is required for the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/eclipse-workspace/spring-boot-h2-integration/target/classes/data.sql]: insert into employee(id,name,email_address) values(1,'Soumitra',''); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "EMPLOYEE" not found; SQL statement:

So when your application starts the database table does not exist but the data.sql file is in the class path folder so spring tries to execute the data.sql file and insert statements fail to execute.

Repository Interface

Create repository interface that will extend Spring Data JPA’s JpaRepository interface to get the default functionalities from the Spring API.

package com.roytuts.spring.boot.h2.integration.repository;

public interface EmployeeRepository extends JpaRepository<Employee, Integer> {

	Employee findByName(String name);

	Employee findByEmail(String email);


In the above repository interface, I have declared two methods for finding the employee either by name or email.

REST Controller

Create Spring REST controller class to retrieve the data from H2 database – employee table.


public class EmployeeRestController {

	private EmployeeRepository employeeRepository;

	public ResponseEntity<Employee> getEmployeeByName(@PathVariable String name) {
		Employee employee = employeeRepository.findByName(name);
		return new ResponseEntity<Employee>(employee, HttpStatus.OK);

	public ResponseEntity<Employee> getEmployeeByEmail(@PathVariable String email) {
		Employee employee = employeeRepository.findByEmail(email);
		return new ResponseEntity<Employee>(employee, HttpStatus.OK);


In the above class I have defined two methods to find the employee when name or email is passed to the path parameter of the URI.

Main Class

Create the main class in order to start up and deploy the Spring Boot application into embedded Tomcat server.

package com.roytuts.spring.boot.h2.integration;

@EntityScan(basePackages = "com.roytuts.spring.boot.h2.integration.entity")
@EnableJpaRepositories(basePackages = "com.roytuts.spring.boot.h2.integration.repository")
public class SpringH2App {

	public static void main(String[] args) {, args);


In the above class I have let the Spring Boot application know where to look for Entity class and Repository interface.

Testing the Spring Boot H2 Integration

Run the main class and your application will be started and deployed into Tomcat server. You will find some important messages in the console, such as:

Finished Spring Data repository scanning in 113ms. Found 1 repository interfaces.
Hibernate: drop table employee if exists
Hibernate: create table employee (id integer generated by default as identity, email_address varchar(255), name varchar(255), primary key (id))
Tomcat started on port(s): 8080 (http) with context path ''

When you hit the URL – http://localhost:8080/employee/name/Soumitra, the corresponding response is:


The corresponding output in the console will be something similar to the below:

Hibernate: select as id1_0_, employee0_.email_address as email_ad2_0_, as name3_0_ from employee employee0_ where

When you hit the URL – http://localhost:8080/employee/email/, the corresponding response you get as:


The corresponding output in the console will be something similar to the below:

Hibernate: select as id1_0_, employee0_.email_address as email_ad2_0_, as name3_0_ from employee employee0_ where employee0_.email_address=?

H2 Database

Now if you hit the URL http://localhost:8080/h2console in the browser then you will see the following page as shown in the image:

integrate in-memory H2 database with Spring Boot

If you see jdbc:h2:~/test in the JDBC URL: section then change it to jdbc:h2:mem:roytuts.

Click on Connect button to connect to the database server. Even you can test the connectivity using Test Connection button before you click on the Connect button.

You will see the following page:

integrate in-memory H2 database with Spring Boot

So the employee table has been created and sequences has also been created for auto generated id field.

Now you can verify data which were inserted through data.sql file:

integrate in-memory H2 database with Spring Boot

Source Code


1 thought on “Integrate H2 In-memory Database with Spring Boot

  1. Hi Soumitra ,
    Thanks for the excellent post .
    i would like to know where you did put your data.sql ? which location ? any setting , props to let spring know we want to load data.sql file ?

Leave a Reply

Your email address will not be published. Required fields are marked *