Spring Boot & H2 - Overview

What is H2?

H2 database is an open source, embedded and in memory relational database management system. It is written in Java and provides a client/server application. It stores data in system memory instead of disk. Once program is closed, data is also lost. An in memory database is used when we don't want to persist the data and unit test the overall functionality. Some of the other popular in memory databases are HSQLDB or HyperSQL Database and Apache Derby. H2 is the most popular one among other embedded databases.

Advantages of H2 Database

Following is the list of advantages that H2 provides −

  • No configuration − Spring Boot intrinsically supports H2 and no extra configuration required to configure H2 database.

  • Easy to Use − H2 Database is very easy to use.

  • Lightweight and Fast − H2 database is very lightweight and being in memory, it is very fast.

  • Switch configurations − Using profiles, you can easily switch between production level database and in-memory database.

  • Supports Standard SQL and JDBC − H2 database supports almost all the features of Standard SQL and operations of JDBC.

  • Web Based Console − H2 Database can be managed by its web based console application.

Configuring H2 Database

Add H2 Database as maven dependency and that's it.


Although, spring boot configures H2 database automatically. We can override the default configurations by specifying them in application.properties as shown below.


Persisting H2 Data

If persistent storage is needed than add the following configuration in application.properties.