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:
If you're using Gradle, add the following line to your build.gradle file:
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:
Explanation of the properties:
spring.datasource.url: The JDBC URL to connect to the H2 database. Thefile:prefix ensures that the database is file-based, rather than in-memory. The./data/testdbindicates the database's file path.spring.datasource.driver-class-name: The fully qualified name of the H2 database driver.spring.datasource.usernameandspring.datasource.password: These represent default credentials for connecting to the database. In this case,sastands for system administrator without a pre-configured password.spring.h2.console.enabled: This boolean flag is set totrueto 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 Point | Setting | Description |
| Dependency Management | com.h2database:h2 | Include in pom.xml or build.gradle. |
| JDBC URL | jdbc:h2:file:./data/testdb | Sets the database to file-based. |
| Driver | org.h2.Driver | Specifies the class name for the driver. |
| Default Username and Password | sa and password | Used for authentication. |
| H2 Console | spring.h2.console.enabled=true | Enables access to the web console. |
Additional Details
- Database Initialization: You can further configure database initialization through
schema.sqlanddata.sqlfiles placed in theresourcesfolder. 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.propertiesandapplication-prod.propertiesfor 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.

