spring-boot
H2 database
file-based configuration
Java development
application setup

How to configure spring-boot to use file based H2 database

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Spring Boot is designed to facilitate rapid development by providing default configurations and features that allow developers to focus on generating business functions. One of the components of Spring Boot that simplifies development is its integration with databases, such as H2. H2 is an open-source, lightweight, and highly performant relational database management system that supports in-memory and persistent modes. This article will guide you through configuring Spring Boot to use a file-based H2 database.

Configuring Spring Boot for a File-based H2 Database

Step 1: Add Maven Dependency

To start using H2 with Spring Boot, you need to include the H2 dependency in your pom.xml file if you're using Maven:

xml
1<dependency>
2    <groupId>com.h2database</groupId>
3    <artifactId>h2</artifactId>
4    <scope>runtime</scope>
5</dependency>

If you're using Gradle, add the following line to your build.gradle file:

gradle
runtimeOnly 'com.h2database:h2'

Step 2: Configure Database Properties

Spring Boot automatically configures an in-memory H2 database if a database connection is not specified. To switch to a file-based H2 database, alter your application.properties (or application.yml) file to specify the database's connection URL. Here's an example using application.properties:

properties
1spring.datasource.url=jdbc:h2:file:./data/testdb
2spring.datasource.driver-class-name=org.h2.Driver
3spring.datasource.username=sa
4spring.datasource.password=password
5spring.h2.console.enabled=true

Explanation of the properties:

  • spring.datasource.url: The JDBC URL to connect to the H2 database. The file: prefix ensures that the database is file-based, rather than in-memory. The ./data/testdb indicates the database's file path.
  • spring.datasource.driver-class-name: The fully qualified name of the H2 database driver.
  • spring.datasource.username and spring.datasource.password: These represent default credentials for connecting to the database. In this case, sa stands for system administrator without a pre-configured password.
  • spring.h2.console.enabled: This boolean flag is set to true to enable the H2 database console for running SQL queries via a web interface.

Step 3: Accessing the H2 Database Console

Once the file-based setup is complete, you can access the H2 console to interact with your database. By default, the console is accessible at http://localhost:8080/h2-console.

To log into the console:

  • JDBC URL: jdbc:h2:file:./data/testdb
  • User Name: sa
  • Password: leave it empty unless changed in the properties.

Ensure that your application server is running to access the H2 console.

Considerations and Best Practices

Data Persistence

While in-memory databases lose their data on application restart, file-based databases save their data to disk, ensuring data persistence across sessions.

File Location

Relative paths can be used for development; however, for production, consider using absolute paths or environment-specific configurations.

Table: Summary of Key Configuration Points

Configuration PointSettingDescription
Dependency Managementcom.h2database:h2Include in pom.xml or build.gradle.
JDBC URLjdbc:h2:file:./data/testdbSets the database to file-based.
Driverorg.h2.DriverSpecifies the class name for the driver.
Default Username and Passwordsa and passwordUsed for authentication.
H2 Consolespring.h2.console.enabled=trueEnables access to the web console.

Additional Details

  • Database Initialization: You can further configure database initialization through schema.sql and data.sql files placed in the resources folder. Spring Boot will automatically execute these scripts at startup to set up the database schema and initial data.
  • Spring Profiles: It's beneficial to leverage Spring profiles to separate configurations for different environments (development, testing, production). You can create files like application-dev.properties and application-prod.properties for environment-specific configurations.
  • Language Support: H2 supports various SQL syntax features and functions, making it suitable for testing various SQL scenarios.

Configuring Spring Boot with a file-based H2 database is straightforward, providing an efficient way to manage data persistence for both development and testing environments. By following this guide, you can leverage the power of H2 for your Spring Boot applications without intricate setup, while maintaining the flexibility to adapt configurations as necessary.


Course illustration
Course illustration

All Rights Reserved.