Persistence top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Introduction

In this article, we'll learn how to configure the Spring Boot application to use the embedded H2 database and then see where H2's embedded database stores the data.

H2 database is a lightweight and open-source database with no commercial support at this point. We can use it in various modes:

  • server mode – for remote connections using JDBC or ODBC over TCP/IP
  • embedded mode – for local connections that use JDBC
  • mixed-mode – this means that we can use H2 for both local and remote connections

H2 can be configured to run as an in-memory database, but it can also be persistent, e.g., its data will be stored on disk. For the purpose of this tutorial, we'll be working with the H2 database in embedded mode with enabled persistence so we'll have data on the disk.

2. Embedded H2 Database

If we want to use the H2 database, we'll need to add the h2 and spring-boot-starter-data-jpa Maven dependencies to our pom.xml file:

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

3. H2's Embedded Persistence Mode

We already mentioned that H2 could use a file system to store database data. The biggest advantage of this approach comparing to the in-memory one is that database data is not lost after the application restarts.

We're able to configure storage mode through the spring.datasource.url property in our application.properties file. This way, we're able to set the H2 database to use the in-memory approach by adding the mem parameter in the data source URL, followed by database name:

spring.datasource.url=jdbc:h2:mem:demodb

If we use the file-based persistence mode, we'll set one of the available options for disk locations instead of the mem parameter. In the next section, we'll discuss what these options are.

Let's see which files does the H2 database create:

  • demodb.mv.db – unlike the others, this file is always created and it contains data, transaction log, and indexes
  • demodb.lock.db – it is a database lock file and H2 recreates it when the database is in use
  • demodb.trace.db – this file contains trace information
  • demodb.123.temp.db – used for handling blobs or huge result sets
  • demodb.newFile – H2 uses this file for database compaction and it contains a new database store file
  • demodb.oldFile – H2 also uses this file for database compaction and it contains old database store file

4. H2's Embedded Database Storage Location

H2 is very flexible concerning the storage of database files. At this moment, we can configure its storage directory to:

  • directory on disk
  • current user directory
  • current project directory or working directory

4.1. Directory on Disk

We can set a specific directory location where our database files will be stored:

spring.datasource.url=jdbc:h2:file:C:/data/demodb

Notice that in this connection string, the last chunk refers to the database name. Also, even if we miss the file keyword in this data source connection URL, H2 will manage it and create files in the provided location.

4.2.  Current User Directory

In case we want to store database files in the current user directory, we'll use the data source URL that contains a tilde (~) after the file keyword:

spring.datasource.url=jdbc:h2:file:~/demodb

For example, in Windows systems, this directory will be C:/Users/<current user>.

To store database files in the subdirectory of the current user directory:

spring.datasource.url=jdbc:h2:file:~/subdirectory/demodb

Notice that if the subdirectory does not exist, it will be created automatically.

4.3. Current Working Directory

The current working directory is one where the application is started, and it's referenced as a dot (.) in the data source URL. If we want database files there, we'll configure it as follows:

spring.datasource.url=jdbc:h2:file:./demodb

To store database files in the subdirectory of the current working directory:

spring.datasource.url=jdbc:h2:file:./subdirectory/demodb

Notice that if the subdirectory does not exist, it will be created automatically.

5. Conclusion

In this short tutorial, we discussed some aspects of the H2 database and showed where H2's embedded database stores the data. We also learned how to configure the location of the database files.

The complete code sample is available over on GitHub.

Persistence bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
guest
0 Comments
Inline Feedbacks
View all comments