BackEnd๐Ÿงต

H2 Database + Spring JPA ์—ฐ๋™

hae02y 2023. 11. 4. 16:09
๋ฐ˜์‘ํ˜•

H2 DB

H2 DB๋Š” ์ž๋ฐ” ๊ธฐ๋ฐ˜์˜ ์˜คํ”ˆ์†Œ์Šค ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์ด๋‹ค. ๋ณดํ†ต ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„๋‚˜ ์ž‘์€ ๋‹จ๊ณ„์—์„œ ์‚ฌ์šฉ์ด ๋˜๋ฉฐ, Gradle์ด๋‚˜ Maven์— ์˜์กด์„ฑ๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋น ๋ฅด๊ฒŒ ์ด์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

์ด๋Ÿฌํ•œ H2 DB ๋Š” 3๊ฐ€์ง€ ๋ชจ๋“œ๋กœ ์ง€์›๋œ๋‹ค.

  • In-memory
  •  Embedded Mode
  •  Server Mode

Inmemory mode

WAS ๊ตฌ๋™์‹œ์— H2 DB์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ ค์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. WAS๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด, ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€์žˆ๋˜ ๋ชจ๋“ ๋ฐ์ดํ„ฐ๊ฐ€ ํœ˜๋ฐœ์„ฑ์œผ๋กœ ์‚ฌ๋ผ์ง„๋‹ค. ์ฆ‰ ์ด๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ํœ˜๋ฐœ์„ฑ์œผ๋กœ ํ…Œ์ŠคํŠธ๋“ฑ์— ์‚ฌ์šฉํ•˜๊ธฐ ์ ํ•ฉํ•˜๋‹ค.

 

Embedded Mode

WAS ๊ตฌ๋™์‹œ์— H2 DB๋ฅผ PC์˜ ๋””์Šคํฌ์— ์ €์žฅํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. In-memory์™€ ๋‹ฌ๋ฆฌ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ๋ผ์ง€์ง€ ์•Š๋Š” ๋น„ํœ˜๋ฐœ์„ฑ๋ชจ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— , ๊ฐœ๋ฐœ ์ดˆ๊ธฐ์— ์‚ฌ์šฉํ•˜๊ธฐ ์ ํ•ฉํ•˜๋‹ค.

 

 

์‚ฌ์šฉ๋ฐฉ๋ฒ•

1. ์˜์กด์„ฑ ์ถ”๊ฐ€

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    
    runtimeOnly 'com.h2database:h2' //h2 ์˜์กด์„ฑ ์ถ”๊ฐ€
    
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ๋‚˜์˜ ๊ฒฝ์šฐ์—๋Š” H2, JDBC, JPA ๋“ฑ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

 

 

2. application.yml(properties) ์„ค์ •

yml์ด๋‚˜ properties์— ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด์ฃผ์ž. ๋‚˜์˜ ๊ฒฝ์šฐ์—๋Š” yml๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

server:
  port: 80 # ๋‚ด์žฅ ํ†ฐ์บฃ ํฌํŠธ๋ฒˆํ˜ธ

spring:
  # H2 Database ์„ค์ •
  datasource:
    driver-class-name: org.h2.Driver
    url: 'jdbc:h2:mem:test'   # H2 DB ์—ฐ๊ฒฐ ์ฃผ์†Œ (In-Memory Mode)
    #url: 'jdbc:h2:~/test'    # H2 DB ์—ฐ๊ฒฐ ์ฃผ์†Œ (Embedded Mode)
    username: username        # H2 DB ์ ‘์† ID (์‚ฌ์šฉ์ž ์ง€์ •)
    password: password        # H2 DB ์ ‘์† PW (์‚ฌ์šฉ์ž ์ง€์ •)

  # H2 Console ์„ค์ •
  h2:
    console: # H2 DB๋ฅผ ์›น์—์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ
      enabled: true           # H2 Console ์‚ฌ์šฉ ์—ฌ๋ถ€
      path: /h2-console       # H2 Console ์ ‘์† ์ฃผ์†Œ

  # JPA ์„ค์ •
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: create        # DB ์ดˆ๊ธฐํ™” ์ „๋žต (none, create, create-drop, update, validate)
    properties:
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect
        format_sql: true      # ์ฟผ๋ฆฌ ๋กœ๊ทธ ํฌ๋งท (์ •๋ ฌ)
        show_sql: true        # ์ฟผ๋ฆฌ ๋กœ๊ทธ ์ถœ๋ ฅ

 

spring.datasource.url

H2 DB๋Š” url์„ ๊ธฐ์ค€์œผ๋กœ ์œ„์—์„œ ์„ค๋ช…ํ–ˆ๋˜ 2๊ฐ€์ง€ ๋ชจ๋“œ๋ฅผ ์„ค์ •๊ฐ€๋Šฅํ•˜๋‹ค. 

url์— `mem`์ด ๋ถ™๊ฒŒ๋œ๋‹ค๋ฉด H2 DB๊ฐ€ In-Memory ๋ชจ๋“œ๋กœ ์ž‘๋™ํ•˜๊ณ , `~`๋กœ ์„ ์–ธ์„ ํ•˜๋ฉด Embedded ๋ชจ๋“œ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค.

 

 

jpa.hibernate.ddl-auto

์˜ต์…˜์— ๋”ฐ๋ผ์„œ Entitiy ํด๋ž˜์Šค์˜ DDL ์ฟผ๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์‹คํ–‰ํ• ๊ฒƒ์ธ์ง€ ์•„๋‹Œ์ง€, ์–ด๋–ค ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ• ๊ฒƒ์ธ์ง€ ๋“ฑ์„ ์„ค์ •ํ• ์ˆ˜์žˆ๋‹ค.

์˜ต์…˜ ์„ค๋ช…
none ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์ „๋žต์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” ํ•ด๋‹น ์˜ต์…˜์ด ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค.
create ๊ธฐ์กด ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•œ ํ›„ ํ…Œ์ด๋ธ”์„ ์žฌ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
create-drop ๊ธฐ์กด ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•œ ํ›„ ํ…Œ์ด๋ธ”์„ ์žฌ์ƒ์„ฑํ•˜๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ ์‹œ์ ์— ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
update ๊ธฐ์กด ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ์œ ์ง€ํ•˜๊ณ , ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ์ปฌ๋Ÿผ(๋ฉค๋ฒ„)๋งŒ ํ…Œ์ด๋ธ”์— ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด ์ปฌ๋Ÿผ(๋ฉค๋ฒ„)์€ ์†์„ฑ์ด ๋ณ€๊ฒฝ๋˜๋”๋ผ๋„ ํ…Œ์ด๋ธ”์— ๋ฐ˜์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
validate DDL ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ , ์˜จ์ „ํžˆ ์—”ํ‹ฐํ‹ฐ์™€ ํ…Œ์ด๋ธ”์ด ์ •์ƒ์ ์œผ๋กœ ๋งคํ•‘๋˜์—ˆ๋Š”์ง€๋งŒ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. none๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์šด์˜ ํ™˜๊ฒฝ์—์„œ ๊ถŒ์žฅ๋˜๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค.

 

 

3. ์‚ฌ์šฉ

๋‚˜์˜ ๊ฒฝ์šฐ์—๋Š” Inmemory ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€๋‹ค. WAS๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ , locahost:8080/h2 ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ ‘์†ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด h2 DB์˜ ์ฝ˜์†”๋ชจ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

 

์ถ”๊ฐ€ : Embedded Mode

Embedded ๋ชจ๋“œ๋Š” PC์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ์‚ญ์ œํ•˜๊ธฐ ์ „๊นŒ์ง€ ์‚ญ์ œ๋˜์ง€์•Š๋Š”๋‹ค. ๊ทธ๋ž˜์„œ ๋ช‡๊ฐ€์ง€ ์„ค์ •์„ ๋”ํ•ด๋ณด์ž.

server:
  port: 80 # ๋‚ด์žฅ ํ†ฐ์บฃ ํฌํŠธ๋ฒˆํ˜ธ

spring:
  # H2 Database ์„ค์ •
  datasource:
    driver-class-name: org.h2.Driver
    #url: 'jdbc:h2:mem:test'  # H2 DB ์—ฐ๊ฒฐ ์ฃผ์†Œ (In-Memory Mode)
    url: 'jdbc:h2:~/test'     # H2 DB ์—ฐ๊ฒฐ ์ฃผ์†Œ (Embedded Mode)
    username: username        # H2 DB ์ ‘์† ID (์‚ฌ์šฉ์ž ์ง€์ •)
    password: password        # H2 DB ์ ‘์† PW (์‚ฌ์šฉ์ž ์ง€์ •)
    
  # H2 Console ์„ค์ •
  h2:
    console: # H2 DB๋ฅผ ์›น์—์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ
      enabled: true           # H2 Console ์‚ฌ์šฉ ์—ฌ๋ถ€
      path: /h2-console       # H2 Console ์ ‘์† ์ฃผ์†Œ

  # JPA ์„ค์ •
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update        # DB ์ดˆ๊ธฐํ™” ์ „๋žต (none, create, create-drop, update, validate)
    properties:
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect
        format_sql: true      # ์ฟผ๋ฆฌ ๋กœ๊ทธ ํฌ๋งท (์ •๋ ฌ)
        show_sql: true        # ์ฟผ๋ฆฌ ๋กœ๊ทธ ์ถœ๋ ฅ

 

  • DDL-AUTO ๋ฅผ UPDATE ๋กœ ๋ณ€๊ฒฝ
  • H2 ์—ฐ๊ฒฐ ์ฃผ์†Œ๋ฅผ ~ ๋กœ ๋ณ€๊ฒฝ

์ด๋ ‡๊ฒŒ ๋‘๊ฐ€์ง€๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค. ์™œ๋ƒํ•˜๋ฉด CREATE ๋กœ DDL-AUTO๋ฅผ ์„ค์ •ํ•˜๋ฉด DB๊ฐ€ ์ƒ์„ฑ๋ ๋•Œ ๋งˆ๋‹ค ์ „์— ์žˆ๋˜ ๊ฒƒ๋“ค์„ ์‚ญ์ œ ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฉ”๋ชจ๋ฆฌ๋กœ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ๊ณผ ๋‹ค๋ฅผ๊ฒƒ์ด ์—†๊ธฐ๋•Œ๋ฌธ์ด๋‹ค.

 

H2 DB ํŒŒ์ผ ์œ„์น˜

์ด๋ ‡๊ฒŒ Embedded๋กœ ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์ŠคํŒŒ์ผ์€ PC์˜ ๋””์Šคํฌ์— ์ €์žฅ์ด ๋œ๋‹ค. ๋‚˜๋Š” ์œˆ๋„์šฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ , ์œˆ๋„์šฐ์—์„œ ๊ฐ€๋ณธ์ ์œผ๋กœ ์ž‘์„ฑ๋˜๋Š” ๊ณณ์€ C:/Users/์‚ฌ์šฉ์ž ์— ์ƒ์„ฑ์ด ๋œ๋‹ค. ์ด๋ฅผ ์ดˆ๊ธฐํ™” ํ•˜๊ณ  ์‹ถ์œผ๋ฉด 

 

.h2.server.properties, test.mv.db ๋ฅผ ์‚ญ์ œํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

๋ฐ˜์‘ํ˜•