Spring Boot
H2 Database
JDBC Connection
H2 Console
Database Configuration

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.

Practice system design

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

xml
1<dependency>
2    <groupId>com.h2database</groupId>
3    <artifactId>h2</artifactId>
4    <scope>runtime</scope>
5</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:

properties
1spring.datasource.platform=h2
2spring.datasource.url=jdbc:h2:mem:testdb
3spring.datasource.driver-class-name=org.h2.Driver
4spring.datasource.username=sa
5spring.datasource.password=

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:

properties
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

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:

java
1@Entity
2public class Book {
3    @Id
4    @GeneratedValue(strategy = GenerationType.AUTO)
5    private Long id;
6    private String title;
7    private String author;
8
9    // Getters and Setters
10}

To create a simple REST API for managing books, you can implement a BookController:

java
1@RestController
2@RequestMapping("/books")
3public class BookController {
4
5    @Autowired
6    private BookRepository bookRepository;
7
8    @GetMapping
9    public List<Book> getAllBooks() {
10        return bookRepository.findAll();
11    }
12
13    @PostMapping
14    public Book createBook(@RequestBody Book book) {
15        return bookRepository.save(book);
16    }
17}

The BookRepository interface can extend JpaRepository to handle CRUD operations:

java
public interface BookRepository extends JpaRepository<Book, Long> {}

Summary Table

The following table summarizes the key configurations for using H2 with Spring Boot:

FeatureConfiguration&#10/Description
Default JDBC URLjdbc:h2:mem:testdb
Driver Classorg.h2.Driver
Default Usernamesa
Default Password(blank)
Enable H2 Consolespring.h2.console.enabled=true
H2 Console URLhttp://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/&#126;/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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.