Post

(Spring) DB 관련 - H2 설정

build.gradle에 다음을 추가.
1
2
3
4
5
runtimeOnly 'com.h2database:h2'

// runtimeOnly 'com.h2database:h2:1.4.193'

  • 반드시 버전을 명시해줄 것.
    • 버전 명시하지 않을 시 최신 버전이 설치되는데, 콘솔에서 connect해보면 자동으로 db 파일을 생성할 수 없어서 에러가 발생한다. :mem:을 써서 인메모리에 하든, :file:을 써서 파일로 저장하든 관계없이 에러가 발생함. 이는어떤 취약점 때문에자동으로 파일 생성하는걸 막아둬서 그렇다고 함. IFEXISTS 플래그를 주면 된다고 나와있는데 줘도 안됨.
    • Database “mem:testdb” not found, and IFEXISTS=true, so we cant auto-create it [90146-199] 90146/90146 (Help)
  • 또 최신 버전으로 바꿔서 해보니까 잘 되네… 뭐가 문제였을까…
application.properties에 다음을 추가.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

spring.datasource.url
=
jdbc:h2:mem:testdb;MODE=MySQL;DB\_CLOSE\_DELAY=-1;DB\_CLOSE\_ON\_EXIT=FALSE
   
spring.datasource.username
=
sa
   
spring.datasource.password
=
   
spring.datasource.driver-class-name
=
org
.
h2
.
Driver



## member\_key 같은 컬럼을 memberKey 필드에 자동으로 매핑되게 하려면 아래 라인도 적어준다.


mybatis.configuration.map-underscore-to-camel-case
=
true


그리고 이후 프로젝트 실행 시 자동으로 테이블 생성하고 mock 데이터 넣는 것은 다음 링크를 참조. 틀리게 나와있는 곳이 너무 많은데, 아래 링크에 정확하게 나와있다.

https://stackoverflow.com/questions/38040572/spring-boot-loading-initial-data

다음 주소로 접속하면 콘솔에 붙을 수 있음.

1
2
3
4

localhost:8080/h2-console

DB_CLOSE_DELAY, DB_CLOSE_ON_EXIT에 대한 설명은 공식 docs 참조

H2에서의 ACID

http://www.h2database.com/html/advanced.html#acid

H2에서는 @Transactional(SERIALIZABLE)로 설정해도 deadlock이 안걸린다.
This post is licensed under CC BY 4.0 by the author.