어린왕자이야기

spring-boot와 h2 사용하기 본문

나의 취미/자바 이야기

spring-boot와 h2 사용하기

grandguy 2022. 5. 5. 15:02

스프링부트 프로젝트를 하면 제일 많이 사용하는 데이터베이스가 우선은 h2 database인거 같다.

h2 database를 사용하면서 오류에 대해서 정리를 해 보자.

 

1. Windows에서 임베디드 모드를 사용할때는 디렉토리 delimeter를 주의

  spring.datasource.url=jdbc:h2:C:\local\programs\h2\bin\sample\spring

  spring.datasource.url=jdbc:h2:C:/local/programs/h2/bin/sample/spring

2. 오류발생 : org.h2.jdbc.JdbcSQLNonTransientConnectionException: File corrupted while reading record: null. Possible solution: use the recovery tool [90030-200]

  h2 database를 서버모드로 사용할때는 자동적으로 파일을 생성할수 없으므로 인위적으로 생성할 필요가 있다. 하지만, 이때 h2 v2모드로 만들어지면 스프링부트의 기본인 h2 v1에서는 읽을수가 없어서 발생하는 오류이다.

따라서 pom.xml을 수정해서 스프링부트에서도 h2.v2를 읽을수 있도록 수정해야 한다.

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <version>2.1.212</version>
  <scope>runtime</scope>
</dependency>