Spring Boot H2 Database Error ) The file is locked: H2 db file

반응형

Error

The file is locked: H2 db file

Caused by: org.h2.mvstore.MVStoreException: The file is locked: C:/Users/Eunbyeol/local.mv.db [2.1.214/7]
	at org.h2.mvstore.DataUtils.newMVStoreException(DataUtils.java:1004) ~[h2-2.1.214.jar:2.1.214]
	at org.h2.mvstore.FileStore.open(FileStore.java:178) ~[h2-2.1.214.jar:2.1.214]
	at org.h2.mvstore.FileStore.open(FileStore.java:128) ~[h2-2.1.214.jar:2.1.214]
	at org.h2.mvstore.MVStore.<init>(MVStore.java:452) ~[h2-2.1.214.jar:2.1.214]
	at org.h2.mvstore.MVStore$Builder.open(MVStore.java:4082) ~[h2-2.1.214.jar:2.1.214]
	at org.h2.mvstore.db.Store.<init>(Store.java:136) ~[h2-2.1.214.jar:2.1.214]
	... 132 common frames omitted

해결 방법

h2에 사용되는 DB파일이 이미 실행되어 해당 파일을 열 수 없다는 뜻

스프링 부트에 실행되어 있는 해당 프로젝트의 실행을 중단한다.

나같은 경우에는 로컬로 해당 서버를 실행한 상태에서 테스트로 H2 데이터베이스에 값을 입력하였다.

로컬 서버를 실행한 상태에서 테스트를 진행해보려면?


해결방법 2

  • application.properties 파일의 h2 설정을 추가
    • 내가 설정한 h2 설정 코드
# DATABASE
# 콘솔 접속 허용 여부
spring.h2.console.enabled=true
# 로컬 DB 콘솔 접속 URL = http://localhost:8080/h2
spring.h2.console.path=/h2
# DB 접속 JDBC URL
spring.datasource.url=jdbc:h2:~/local
# 데이터베이스 접속에 사용되는 드라이버
spring.datasource.driverClassName=org.h2.Driver
# 데이터베이스의 사용자명
spring.datasource.username=sa
# 데이터베이스의 사용자 비밀번호
spring.datasource.password=1234

 

  • 추가해야 할 코드
    • datasource.url = AUTO_SERVER = true
# DB 접속 JDBC URL
spring.datasource.url=jdbc:h2:~/local;AUTO_SERVER=true

 

  • 설정하였더니 실행 잘 됨

 

  • 수정된 전체 코드
# DATABASE
# 콘솔 접속 허용 여부
spring.h2.console.enabled=true
# 로컬 DB 콘솔 접속 URL = http://localhost:8080/h2
spring.h2.console.path=/h2
# DB 접속 JDBC URL
spring.datasource.url=jdbc:h2:~/local;AUTO_SERVER=true
# 데이터베이스 접속에 사용되는 드라이버
spring.datasource.driverClassName=org.h2.Driver
# 데이터베이스의 사용자명
spring.datasource.username=sa
# 데이터베이스의 사용자 비밀번호
spring.datasource.password=1234
반응형