H2 is a lightweight in memory relational database management system that is commonly used with spring boot for development which greatly supports integration testing.
There are numerous uses of H2 Database that makes it user friendly some of them are:
Great for unit and integration testing:
Every time test suite runs H2 creates a fresh database schema.
It works with @SpringBootTest and @DataJpaTest seamlessly.
Fast and lightweight
Since it is an in memory relational database it is super fast in read/write operation.
No need to install a heavy database server.
Supports both in memory and file mode
By using spring.datasource.url=jdbc:h2:mem:testdb we can make sure the db exists only while the app is up and running.
spring.datasource.url=jdbc:h2:file:./data/testdb. persists the data into a local file.
Easy debugging with web console
H2 comes with a built in web console /h2-console.
It is handy to run SQL queries and check if data is persisted correctly.
How to access web console:
After starting your Spring Boot application, you can access the H2 console by navigating to http://localhost:8080/h2-console in your web browser.
Setting up H2 Database for a spring-boot application:
Add H2 Dependency
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
Based on the application requirement scope of H2 Database can be changed. Most commonly used scopes are compile, runtime and test.
compile :
It's a default scope if you don't specify one.
Dependency is available at compile time, runtime and packaged into your app.
runtime:
Not required during compile time but needed at runtime.
JDBC drivers don't refer them directly in code but they are required during running the app.
test:
- Available only during test compilation and execution.
Configure application properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto this property tells hibernate what to do with the database schema each time the application starts.
create-drop: Hibernate will create the schema when the application starts and drops all the tables on shut down. Perfect for testing , but not for real world applications where data has to be retained.
update: Hibernate will update the schema to match entities. It will keep existing data in the table. New data/table will be added to the database but the old data won't be deleted automatically. Good for developing small scale applications but risky in production.
Conclusion:
The H2 database is a powerful tool for quickly testing and prototyping Spring Boot applications. With minimal configuration, it allows you to spin up an in memory or file based database, visualize data through the H2 Console, and run multi table queries just like you would in a production grade database.
Choosing the right spring.jpa.hibernate.ddl-auto option whether update for retaining data across restarts or create-drop for fresh setups can make development smoother and testing faster. By leveraging H2, you can isolate and validate application logic without worrying about external dependencies, making it a must have for both beginners and seasoned Spring developers.
Top comments (0)