Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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 code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW