Java/Spring

[Spring] Testing에서 메모리 DB(MMBB) 사용하기와 메모리 DB란

DSeung 2023. 6. 21. 13:33

프로젝트 개발 중에 테스팅을 운영 디비와 같은 걸로 할 경우 

더미 데이터가 추가되는 데 이를 방지하기 위해 스프링에서는 메모리 디비를 사용할 수 있게 해 줍니다.

 

※ 메모리 DB(In-Memory Database, Main Memory DBMS) : 비휘발성이 아닌 휘발성인 주 메모리에 데이터를 기록하는 방법으로 데이터 조회가 빠르다는 장점을 지니지만 휘발성이므로 전원이 꺼질 시 데이터가 삭제됨, 주로 임시 데이터나 로그인 세션에 이용

DBMS 제품군에서 옵션으로 제공하기도 함 MySQL/MariaDB에서는 MEMORY 엔진이 예시

 

스프링은 테스트 폴더 위치에 resources 폴더가 있으면 그 파일을 우선적으로 사용하게 되는 데(없으면 main 폴더)

이때 src/test/resources/application.yml의 내용을 아래와 같이 바꾸면 스프링에서 메모리 디비를 사용하게 됩니다.

spring:

logging.level:
  org.hibernate.SQL: debug
  org.hibernate.type: trace

더 극적으로는 내용을 아예 다 비워도 됩니다.

 

이러면 테스트 코드 실행 시 memory db를 사용하는 걸 로그로 확인이 가능함

023-06-21 13:22:17.535  INFO 47956 --- [           main] p6spy                                    : #1687321337535 | took 0ms | statement | connection 2| url jdbc:h2:mem:4bfbe2b9-b7f9-44f1-8c78-5ee64d78dcf3
drop table if exists category CASCADE 
drop table if exists category CASCADE ;
2023-06-21 13:22:17.535 DEBUG 47956 --- [           main] org.hibernate.SQL                        : drop table if exists category_item CASCADE 
2023-06-21 13:22:17.535  INFO 47956 --- [           main] p6spy                                    : #1687321337535 | took 0ms | statement | connection 2| url jdbc:h2:mem:4bfbe2b9-b7f9-44f1-8c78-5ee64d78dcf3
drop table if exists category_item CASCADE 
drop table if exists category_item CASCADE ;
2023-06-21 13:22:17.535 DEBUG 47956 --- [           main] org.hibernate.SQL                        : drop table if exists delivery CASCADE 
2023-06-21 13:22:17.535  INFO 47956 --- [           main] p6spy                                    : #1687321337535 | took 0ms | statement | connection 2| url jdbc:h2:mem:4bfbe2b9-b7f9-44f1-8c78-5ee64d78dcf3
drop table if exists delivery CASCADE 
drop table if exists delivery CASCADE ;
2023-06-21 13:22:17.535 DEBUG 47956 --- [           main] org.hibernate.SQL                        : drop table if exists item CASCADE 
2023-06-21 13:22:17.535  INFO 47956 --- [           main] p6spy                                    : #1687321337535 | took 0ms | statement | connection 2| url jdbc:h2:mem:4bfbe2b9-b7f9-44f1-8c78-5ee64d78dcf3
drop table if exists item CASCADE 
drop table if exists item CASCADE ;
2023-06-21 13:22:17.535 DEBUG 47956 --- [           main] org.hibernate.SQL                        : drop table if exists member CASCADE 
2023-06-21 13:22:17.535  INFO 47956 --- [           main] p6spy                                    : #1687321337535 | took 0ms | statement | connection 2| url jdbc:h2:mem:4bfbe2b9-b7f9-44f1-8c78-5ee64d78dcf3
drop table if exists member CASCADE 
drop table if exists member CASCADE ;
2023-06-21 13:22:17.535 DEBUG 47956 --- [           main] org.hibernate.SQL                        : drop table if exists order_item CASCADE 
2023-06-21 13:22:17.535  INFO 47956 --- [           main] p6spy                                    : #1687321337535 | took 0ms | statement | connection 2| url jdbc:h2:mem:4bfbe2b9-b7f9-44f1-8c78-5ee64d78dcf3

 

반응형