Spring Boot default H2 jdbc connection and H2 console
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Spring Boot's integration with the H2 database provides an excellent environment for rapid prototyping and development. H2 is an open-source, lightweight, and embedded database engine that offers a variety of features suited for development and testing environments. This article details the default setup of the JDBC connection to H2 in a Spring Boot application and the H2 console, along with technical explanations and examples.
Default H2 Database Configuration in Spring Boot
Spring Boot simplifies the configuration of the H2 database through sensible defaults. By including the H2 dependency in your pom.xml or build.gradle, Spring Boot auto-configures a JDBC connection to an in-memory H2 database.
Maven Dependency
This configuration places the H2 database on the classpath during the runtime scope to ensure it is only used for development or testing.
JDBC URL
Spring Boot uses a default JDBC URL to create an in-memory database: jdbc:h2:mem:testdb. This URL denotes a transient, in-memory database that exists only in the JVM's memory during the application's lifecycle.
Application Properties
By default, Spring Boot includes a set of properties that govern the H2 database's behavior:
H2 Console
Spring Boot includes the H2 Console, a web-based interface for managing and running queries on the H2 database. The console is accessible typically at the /h2-console endpoint when the application is running.
Enabling the H2 Console
To enable the H2 Console, add the following configuration to the application.properties file:
Accessing the H2 Console
Run your application and navigate to http://localhost:8080/h2-console (replace 8080 with the appropriate port if changed). Use the following credentials to connect:
- JDBC URL:
jdbc:h2:mem:testdb - User Name:
sa - Password: (leave blank)
Customizing the H2 Console Path
You can customize the path of the H2 console by changing the spring.h2.console.path property to a desired endpoint.
Example Usage
Consider a simple Spring Boot application where you have an entity named Book:
To create a simple REST API for managing books, you can implement a BookController:
The BookRepository interface can extend JpaRepository to handle CRUD operations:
Summary Table
The following table summarizes the key configurations for using H2 with Spring Boot:
| Feature | Configuration
/Description |
| Default JDBC URL | jdbc:h2:mem:testdb |
| Driver Class | org.h2.Driver |
| Default Username | sa |
| Default Password | (blank) |
| Enable H2 Console | spring.h2.console.enabled=true |
| H2 Console URL | http://localhost:8080/h2-console |
Additional Considerations
Persistence
By default, the database is in-memory and transient, meaning it will be wiped when the JVM stops. For persistent storage, change the JDBC URL to a file or TCP-based configuration, such as jdbc:h2:file:/path/to/data or jdbc:h2:tcp://localhost/~/test.
Security Considerations
While the H2 console is useful in development, it exposes sensitive data and should not be enabled in production environments. Always secure access with authentication and HTTPS if needed.
Spring Data JPA Integration
Spring Boot also makes it extremely easy to integrate Spring Data JPA with H2, providing access to database operations without boilerplate code. Use annotations like @Entity, @Id, and Spring repositories to handle data logic seamlessly.
Using Spring Boot with H2 offers a convenient way to develop and test data-driven applications swiftly, while providing flexibility for transitioning to other databases for production environments.
Related reading
- Spring boot doesn't load data to initialize database using data.sql
- Spring boot fails to load DataSource using PostgreSQL driver
- Spring Boot Getting Scheduled cron value from database
- Spring Boot Hibernate and Flyway boot order
- Spring Boot default proxying mechanism
- Spring Boot default test throws an IllegalStateException
- Spring Boot JPA2 Hibernate - enable second level cache
- Spring Boot JPA - configuring auto reconnect

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.