Configure Spring Boot with Embedded H2, HSQL, and Derby Databases

In this article, we will discuss how to configure Spring boot application with H2, HSQL, and Derby embedded databases.
It is often convenient to develop applications by using an in-memory embedded database. Obviously, in-memory databases do not provide persistent storage. You need to populate your database when your application starts and be prepared to throw away data when your application ends.
Spring Boot can auto-configure embedded H2, HSQL, and Derby databases. You need not to provide any connection URLs. You need only include a build dependency on the embedded database that you want to use.
Let's first familiar with what is an in-memory database? Typical databases like MySQL or Oracle involve a lot of setups like:
  • Install the Database
  • Setup a Schema
  • Setup the tables
  • Populate the data
  • Connect the application to the database by setting up a data source and a lot of other code
The typical use of in-memory database while client demo or UAT testing or quick R&D or quick POC etc. Using a traditional database involves a lot of overhead.

Some advantages of in-memory databases

  • Zero-project setup or infrastructure
  • Zero Configuration
  • Zero Maintainance
  • Easy to use for Learning, POCs and Unit Tests
Let's discuss how to configure Spring boot with H2, HSQL, and Derby databases.

H2

What is the H2 Database?

H2 is an open source database and is written in Java. It is very fast and of very small size. It is primarily used as an in-memory database which means it stores the data in memory and will not persist data on disk. Although if we need to persist the data, it supports that as well.
The H2 database is not recommended for production environments and is ideal for a quick POC kind of project where there is a need for a simple database.
Spring Boot has very good integration for H2. It's very easy to use H2 database in spring boot applications by just adding the h2 dependency in your pom.xml file:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>
Notice that you need not provide any connection URLs. You need only include a build dependency on the embedded database that you want to use.

Using H2’s Web Console

The H2 database provides a browser-based console that Spring Boot can auto-configure for you. The console is auto-configured when the following conditions are met:
  • You are developing a servlet-based web application.
  • com.h2database:h2 is on the classpath.
  • You are using Spring Boot’s developer tools.
Note: If you are not using Spring Boot’s developer tools but would still like to make use of H2’s console, you can configure the spring.h2.console.enabled property with a value of true.
Note: The H2 console is only intended for use during development, so you should take care to ensure that spring.h2.console.enabled is not set to true in production.

Changing the H2 Console’s Path

By default, the console is available at /h2-console. You can customize the console’s path by using the spring.h2.console.path property.

HSQL

What is HSQLDB?

HSQLDB (HyperSQL DataBase) is the leading SQL relational database software written in Java. It offers a small, fast multithreaded and transactional database engine with in-memory and disk-based tables and supports embedded and server modes. It includes a powerful command line SQL tool and simple GUI query tools.
Official website - http://hsqldb.org/
Spring Boot has very good integration for HSQLDB. It's very easy to use HSQLDB database in spring boot applications by just adding the HSQLDB dependency in your pom.xml file:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <scope>runtime</scope>
</dependency>
Notice that you need not provide any connection URLs. You need only include a build dependency on the embedded database that you want to use.

Derby

What is Apache Derby?

Apache Derby, an Apache DB subproject, is an open source relational database implemented entirely in Java and available under the Apache License, Version 2.0. Some key advantages include:
  • Derby has a small footprint -- about 3.5 megabytes for the base engine and embedded JDBC driver.
  • Derby is based on the Java, JDBC, and SQL standards.
  • Derby provides an embedded JDBC driver that lets you embed Derby in any Java-based solution.
  • Derby also supports the more familiar client/server mode with the Derby Network Client JDBC driver and Derby Network Server.
  • Derby is easy to install, deploy, and use.
Official website - https://db.apache.org/derby/
Spring Boot has very good integration for Derby. It's very easy to use Derby database in spring boot applications by just adding the Derby dependency in your pom.xml file:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <scope>runtime</scope>
</dependency>
Notice that you need not provide any connection URLs. You need only include a build dependency on the embedded database that you want to use.
That's all about spring boot embedded database support.

If you have any suggestion about this article then leave your comments in the comment section.

In the next article, you will learn how to develop CRUD RESTFul API using Spring boot 2, Hibernate 5, JPA, Maven, and MySQL database.
Check out all spring boot articles, guides, and tutorials at Top Spring Boot Tutorials

Comments