Configure H2 Database in Spring Boot Application

Below are the steps to set up H2 database in Spring Boot application.

Step 1: Adding the dependency

To use the H2 database in the spring boot application we have to add the following dependency in the pom.xml file:

h2 and spring-boot-starter-data-jpa dependencies:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

Step 2: Configure Application Properties

Configure the H2 database in the application.properties file:

# H2 Database
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:dcbapp
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

We can also use YAML file for database configuration by adding properties to application.yml file as depicted below:

spring:
h2:
console.enabled: true
datasource:
url: jdbc:h2:mem:dcbapp
driverClassName: org.h2.Driver
username: sa
password: password
jpa:
spring.jpa.database-platform: org.hibernate.dialect.H2Dialect

Let’s understand what these properties are by opening the H2 Database console. 

Spring Boot with H2 Database

H2 Database in Spring Boot is an embedded, open-source, and in-memory database. It is a relational database management system written in Java. It is a client/server application. It stores data in memory, not persist the data on disk. Here we will be discussing how can we configure and perform some basic operations in Spring Boot using H2 Database.

In this article, we will explore the benefits and usage of H2 database in Spring Boot applications. Key features such as ease of setup, configuration, and running SQL queries will be covered.

What is H2 Database?

H2 is a lightweight and fast SQL database written in Java. It can run in two modes: in-memory and embedded. The in-memory mode is particularly useful for testing and development because it allows you to create a temporary database that is automatically destroyed when the application stops. The embedded mode is used for applications that need a small, self-contained database.

Features of the H2 Database:

  • Very fast, open-source, JDBC API
  • Embedded and server modes; disk-based or in-memory databases.
  • Transaction support, multi-version concurrency
  • Browser-based Console application
  • Encrypted databases
  • Fulltext search
  • Pure Java with a small footprint: around 2.5 MB jar file size
  • ODBC driver

Similar Reads

Configure H2 Database in Spring Boot Application

Below are the steps to set up H2 database in Spring Boot application....

Accessing the H2 Console

By default, the console view of the H2 database is disabled. Before accessing the H2 database, we must enable it by using the following property:...

Using H2 Database to perform CRUD Operation in Spring Boot

We are going to perform some basic CRUD Operations by creating a Spring Boot Application and using the H2 Database....

Accessing H2 Database

Below are the properties for the application.properties file....

Conclusion

In this article, we have learned how to configure, access H2 Database and how to test endpoints in Postman for managing the running database....