Basically the database setup involves several steps before we can use it through configured datasource in our application. This actually is required in case of actual project implementations. However, there are situations where we just need a POC to be done for something, and the whole database setup thing still becomes an obligation. Similarly, for unit tests, it would be ideal to have our own set of records in the database, so its independent, without getting impacted by the data changes in the deployed environment. For such use cases, in-memory databases are the ideal solution.

An in-memory database gets created when the application starts, while it gets destroyed when the application stops.

Spring Boot integrates H2 database with lot of ease. So you can easily and quickly switch between a real database and an in-memory database.

Note that, H2 InMemory Database is a relational DBMS written in Java.

Let’s check out a quick demo.


Lets generate the Spring Boot project from Spring initializr. Ensure you add Web, JPA, H2 and DevTools dependencies as shown below.

H2 InMemory Database

Let’s look at the generated pom file.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
	<description>Demo project for Spring Boot</description>
		<relativePath /> <!-- lookup parent from repository -->

We also notice file generated as –

# H2
# Datasource

The property spring.h2.console.enabled=true enables the web console at http://localhost:8080/h2

H2 InMemory Database

Click on Connect and you get into the page where you see available tables in the DB.

H2 InMemory Database

You might be wondering, how come Student table got created. The magic happens with data.sql placed at src/main/resources. Just ensure you have the insert statements in the data.sql file as mentioned below –


insert into STUDENT
values(10001,'Ajay', 'AAA1');
insert into STUDENT
values(10002,'Ajit', 'AAA2');

Spring Boot Auto Configuration checks the values in the data.sql file and does the needful for you, i.e. creates the STUDENT table, and executes the insert statements. Smart!

Lets check out how we can operate on these student records.

To do that, let’s have the Student entity class created now.

package com.jcombat.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
public class Student {
	private Long id;
	private String name;
	private String section;
	public Student() {
	public Student(Long id, String name, String section) { = id; = name;
		this.section = section;
	public Long getId() {
		return id;
	public void setId(Long id) { = id;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getSection() {
		return section;
	public void setSection(String section) {
		this.section = section;

To access the database, let’s write a simple JPA interface which provides the required helper functions to do basic DB operations.

package com.jcombat.repository;
import org.springframework.stereotype.Repository;
import com.jcombat.entity.Student;
public interface StudentRepository extends JpaRepository<Student, Long> {

Let’s now customize the Spring Boot entry point class using Command Line Runner, so we will be able to execute our Spring Boot application from command line.

package com.jcombat.h2demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import com.jcombat.repository.StudentRepository;
public class H2demoApplication implements CommandLineRunner {
	// mvn spring-boot:run
	private Logger LOG = LoggerFactory.getLogger("H2demoApplication");
	StudentRepository studentRepository;
	public H2demoApplication(StudentRepository studentRepository) {
		this.studentRepository = studentRepository;
	public static void main(String[] args) {, args);
	public void run(String... args) throws Exception {"Student count in DB: {}", studentRepository.count());

Executing the application

To run the application, you can either do it directly from your Eclipse IDE, or from command-line through below command –

mvn spring-boot:run

When executed, we see the below output displayed on console –

Download the source code

H2 InMemory Database