Spring Boot
H2 Database
SpringBoot 2.3.0
Java
Database Integration

springboot 2.3.0 while connecting to h2 database

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Connecting Spring Boot 2.3.0 to H2 is usually straightforward, but small configuration mistakes can make it look like the database is broken. In most cases the real issue is one of three things: the dependency is missing, the JDBC URL is wrong, or JPA is starting with settings that do not match the intended database mode.

H2 is especially useful in development and tests because it can run in memory, start quickly, and work with Spring Data JPA using very little setup.

Minimal Dependency Setup

For a Maven project, you typically need Spring Data JPA and H2:

xml
1<dependencies>
2  <dependency>
3    <groupId>org.springframework.boot</groupId>
4    <artifactId>spring-boot-starter-data-jpa</artifactId>
5  </dependency>
6
7  <dependency>
8    <groupId>com.h2database</groupId>
9    <artifactId>h2</artifactId>
10    <scope>runtime</scope>
11  </dependency>
12</dependencies>

If the H2 dependency is missing, Spring Boot cannot create the embedded datasource automatically.

A Working application.properties

For a simple in-memory setup in Spring Boot 2.3.0, this is a solid starting point:

properties
1spring.datasource.url=jdbc:h2:mem:testdb
2spring.datasource.driverClassName=org.h2.Driver
3spring.datasource.username=sa
4spring.datasource.password=
5
6spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
7spring.jpa.hibernate.ddl-auto=update
8
9spring.h2.console.enabled=true
10spring.h2.console.path=/h2-console

This creates an in-memory database named testdb, enables the H2 web console, and lets Hibernate create or update tables.

A simple entity works normally:

java
1import javax.persistence.Entity;
2import javax.persistence.GeneratedValue;
3import javax.persistence.GenerationType;
4import javax.persistence.Id;
5
6@Entity
7public class Book {
8
9    @Id
10    @GeneratedValue(strategy = GenerationType.IDENTITY)
11    private Long id;
12
13    private String title;
14
15    protected Book() {
16    }
17
18    public Book(String title) {
19        this.title = title;
20    }
21}

Understanding In-Memory Versus File Mode

One source of confusion is that H2 can run in memory or in a file. An in-memory URL such as jdbc:h2:mem:testdb disappears when the application stops. A file-based URL persists data between runs.

Example file mode:

properties
spring.datasource.url=jdbc:h2:file:./data/demo-db

If you restart the app and the data vanishes, check whether you accidentally used mem instead of file.

Verifying the Connection

A simple repository is enough to confirm the setup:

java
1import org.springframework.data.jpa.repository.JpaRepository;
2
3public interface BookRepository extends JpaRepository<Book, Long> {
4}

And a startup runner:

java
1import org.springframework.boot.CommandLineRunner;
2import org.springframework.context.annotation.Bean;
3import org.springframework.context.annotation.Configuration;
4
5@Configuration
6public class DataLoader {
7
8    @Bean
9    CommandLineRunner load(BookRepository repository) {
10        return args -> {
11            repository.save(new Book("Spring in Action"));
12            System.out.println(repository.count());
13        };
14    }
15}

If the app starts and prints a count, the datasource, JPA, and entity mapping are all working together.

Typical Problems in Spring Boot 2.3.0

A common error is forgetting the driver class or typing the JDBC URL incorrectly. Another is using schema settings meant for a production database while testing against H2.

For example, some teams use vendor-specific SQL scripts that fail on H2 syntax. In that case, the connection may be fine but schema initialization is failing during startup.

Another frequent issue is console access. The H2 console uses the same JDBC URL as the app. If the application uses jdbc:h2:mem:testdb, the console must use that exact value as well.

H2 Console Access

Once the app is running with the console enabled, open:

text
http://localhost:8080/h2-console

Use:

  • JDBC URL: jdbc:h2:mem:testdb
  • User Name: sa
  • Password: leave blank unless configured

If the console opens but login fails, compare the console URL and credentials with the Spring properties instead of guessing.

Common Pitfalls

The biggest pitfall is assuming H2 behaves exactly like MySQL, PostgreSQL, or another production database. It is useful for development, but SQL differences still matter.

Another issue is expecting in-memory data to survive an application restart. It will not unless you switch to file mode.

Developers also sometimes enable the H2 console but forget Spring Security settings or local network restrictions that may block access to the console path.

Finally, do not confuse successful datasource creation with successful schema execution. The app may connect correctly and still fail because Hibernate mappings or initialization scripts are incompatible.

Summary

  • Spring Boot 2.3.0 works well with H2 when the dependency and JDBC settings are correct.
  • 'jdbc:h2:mem:testdb creates an in-memory database that resets on restart.'
  • Enable the H2 console and use the exact same JDBC URL that the app uses.
  • Validate the setup with a simple entity and repository instead of debugging the whole app at once.
  • If startup fails, separate datasource issues from schema or SQL compatibility issues.

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.