[JAVA] Spring with Kotorin --8 Repository layer --Supplement: H2 Database

Overview / Description

Spring with Kotorin --8 Repository layer provides a repository layer to abstract the data persistence process and create an external persistence area. The operation to is made transparent.

In this case, the external persistence area used the H2 Database Engine, which runs in-memory and embedded in the application.

This H2 Database Engine has a built-in console application for checking database information. Let's check the status of the database using that.

Assumptions / Environment

Runtime version

Spring Dependencies

Development environment

Procedure / Explanation

application.yml settings

Define the configuration information in SpringBoot's application.yml so that the H2 database can be used as follows.

Data source

Implement the data source definition for connecting to the database from Spring application as follows.

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:app;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE
    username: guest
    password: guest
Component Contents
driver-class-name H2DB driver library
org.h2.Driver
url Database connection URL(* Format will be described later)
jdbc:h2:mem:app;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE
username Database connection username
password Database connection password
H2 database connection URL format

The H2 database can take various operation modes such as using an application built-in type that operates in-memory, a server mode that operates as a remote server, and an operation that writes data to a file system. The connection URL format is suitable for each operation mode.

action mode URL format sample
Embedded mode: in-memory(private) jdbc:h2:mem: jdbc:h2:mem:
Embedded mode: in-memory(Named) jdbc:h2:mem:<DB name> jdbc:h2:mem:app
Built-in mode: Local file jdbc:h2:[file:][<File Path>]<DB name> jdbc:h2:file:/data/sample
In-memory DB drop prevention DB_CLOSE_DELAY=-1 jdbc:h2:mem:app;DB_CLOSE_DELAY=-1
Server mode: TCP connection jdbc:h2:tcp://[:]/[] jdbc:h2:tcp://localhost:8084/data/sample
Server mode: TLS connection jdbc:h2:ssl://[:]/[] jdbc:h2:ssl://localhost/mem:test
DB connection disconnection at VM termination DB_CLOSE_ON_EXIT=TRUE jdbc:h2:mem:app;DB_CLOSE_ON_EXIT=TRUE

H2 database

Make the following settings to display the console screen of the H2 database.

spring:
  h2:
    console:
      enabled: true

Displaying the console screen of the H2 database

After launching the application, try accessing the H2 database console screen.

$ ./gradlew bootRun

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)
:
:
:
2019-02-26 16:05:13.358  INFO 7073 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 15 endpoint(s) beneath base path '/admin'
2019-02-26 16:05:13.457  INFO 7073 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-02-26 16:05:13.460  INFO 7073 --- [           main] i.p.s.simple.SimpleApplicationKt         : Started SimpleApplicationKt in 5.577 seconds (JVM running for 5.998)
<==========---> 83% EXECUTING [2m 46s]
> :bootRun

--Access http: // localhost: [set port number] / h2-console and enter the connection user / password of the H2 database to connect.

h2-console-login.png H2-console.png

Summary / Looking back

I tried to operate the H2 database that can be easily used in combination with SpringBoot. In application development situations that do not depend on the database function itself, there may be various situations where an easy-to-use H2 database can be used without installation work.

This source

Recommended Posts

Spring with Kotorin --8 Repository layer --Supplement: H2 Database
Spring with Kotorin --8 Repository layer
Spring with Kotorin ―― 7. Service layer
Spring with Kotorin --9 Database migration --Flyway
Spring with Kotorin --- 5. Actuator
Spring with Kotorin ―― 1. SPRING INITIALIZR
Spring with Kotorin --6 Asynchronous processing
Spring with Kotorin --4 REST API design
Database linkage with doma2 (Spring boot)
Spring with Kotorin --2 RestController and Data Class
DB environment construction with DBFlute Intro + H2 Database
Creating a common repository with Spring Data JPA
Database environment construction with Docker in Spring boot (IntellJ)