스프링부트로 JPA 기반 프로젝트를 개발 시, 로컬 환경에 MariaDB를 설치해 사용했는데, 공용 PC에서 소스만 깃에서 받아 개발을 이어가야 할 경우가 생겼다.
DB가 없는 환경에서 애플리케이션을 임시로 띄워야 하는데 MariaDB가 없어서 아래 오류메시지와 함께 기동이 안 되는 상황이 발생했다.
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
또한 JPA 관련해서 다음과 같은 오류도 이어졌다.
Field menuMDao required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.
◆ 원인
JPA 기반 프로젝트는 Spring Boot 특성상 반드시 DataSource를 요구한다.
그래서 실제 DB가 없는 환경에서는 많은 에러가 발생할 수밖에 없다.
스프링부트는 JPA 프로젝트이기 때문에 무조건 DataSource + EntityManagerFactory가 필요하다고 판단하는데,
현재 서버에는 MariaDB가 없으니 생성할 수 없어 애플리케이션 자체가 뜨지 않는 것이었다.
◆ 해결방안
이를 해결하는 가장 간단하고 안정적인 방법이 바로 H2 메모리 DB를 임시로 붙여주는 방식이다.
즉, MariaDB 대신 H2를 연결해서 JPA를 만족시키는 것인데, 이 방법을 사용하면
실제 DB 없이도 개발 서버 기동이 가능해 지며, JPA / Repository / EntityManager 관련 코드 수정도 불필요하다.
나중에 MariaDB 연결할 때도 설정 파일만 다시 바꿔주면 되기 때문에 간단하다.
1. build.gradle 수정
dependencies {
...
//DB 없는 환경에서 임시로 사용할 H2 메모리 DB 추가
runtimeOnly 'com.h2database:h2'
...
}
2. application.properties – H2 연결 설정
MariaDB 설정은 그대로 주석 처리한 채, 아래처럼 H2 설정만 추가한다.
#H2 임시 메모리 DB 설정 추가
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
글 내용 중 잘못된 부분이 있거나, 첨부하실 내용이 있으시면 댓글로 남겨주세요. 공부하는데 많은 도움이 됩니다.
-- 기억의 유효기간은 생각보다 짧다. --
'프레임워크 > Spring (스프링)' 카테고리의 다른 글
| [Spring] Java와 jQuery로 만드는 이미지 크롭(자르기) 기능 구현 + 예제 (0) | 2025.09.24 |
|---|---|
| [Spring] 개발 생산성 높이는 유용한 JPA 설정 (0) | 2025.06.09 |
| 스프링 부트(Spring Boot)의 세션 만료 시간 (session timeout) 설정 방법 (1) | 2025.01.10 |
| [Spring] MultipartFile 파일 업로드 최대크기 제한 조정 (1) | 2024.10.02 |
| [Spring] MultipartFile 비어있는 파일 확인 - isEmpty() (0) | 2024.07.31 |