Home H2 Database Integration Spring Boot
Post
Cancel

H2 Database Integration Spring Boot

H2 Database Integration With SpringBoot

We’ll explore how to integrate H2 with Spring Boot.

Step 1: Create a New Spring Boot Project

The first step is to create a new Spring Boot project. You can use your favorite IDE or the Spring Initializer website to generate the project. When you’re done, you should have a basic Spring Boot project with the default dependencies.

Step 2: Add the H2 Dependency

The next step is to add the H2 dependency to your project. You can add the following line to your pom.xml file to include the H2 library:

1
2
3
4
5
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

Step 3: Configure the Application Properties

Now that you have the H2 library, you need to configure the application properties to use H2 as the database. You can add the following lines to your application.properties file:

1
2
3
4
5
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=create-drop

H2 DB data can be store in in-momory or file stroge (Persist Data)

Persist H2 Data To configure H2 file storage, you’ll need to modify the URL used to connect to the database. Here’s an example configuration:

1
2
3
4
spring.datasource.url=jdbc:h2:file:./data/testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

With this configuration, the H2 database will be stored in a file named testdb in the data directory relative to the application’s working directory. Data stored in the database will persist across application restarts.

In-momory H2 Data: Here’s an example of how to configure H2 in-memory storage in your Spring Boot application’s application.properties file:

1
2
3
4
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=```

With this configuration, the H2 database will be created in memory when the application starts up. Any data stored in the database will be lost when the application shuts down.

Enable H2 Console

1
spring.h2.console.enabled=true
  • Default console URL:
    1
    2
    
     http://host:port/h2-console 
     eg: (http://localhost:8080/h2-console)
    
  • Change H2 Console Endpoint
1
spring.h2.console.path=/h2-console

H2 Console : Disable spring security for H2 Console

To disable Spring Security for H2 Console in a Spring Boot application, you can add the appropriate configuration in the WebSecurityConfigurerAdapter class. Here is an example:

1
2
3
4
5
6
7
8
9
10
11
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests()
        .antMatchers("/h2-console/**").permitAll()
        .and().headers().frameOptions().disable()
            .and().csrf().ignoringAntMatchers("/h2-console/**")
            .and().csrf().disable();;
    }
}

In this configuration, we are permitting access to all requests for the H2 Console URL (/h2-console/**). Additionally, we are disabling the X-Frame-Options header and CSRF protection for the H2 Console requests.

Note that in a production environment, it’s important to secure access to the H2 Console using proper authentication and authorization mechanisms. Disabling Spring Security for the H2 Console should only be done for development and testing purposes.

Conclusion:

In conclusion, H2 is a powerful and flexible database that can be easily configured in a Spring Boot application. With the right configuration we can enable or disable features.

This post is licensed under CC BY 4.0 by the author.