본문 바로가기

POST/Spring Boot

Spring Boot2.5 data.sql 관련 오류

오류 원인

Springboot가 2.5로 업그레이드되면서 data.sql 파일을 사용해서 초기 dummy 데이터를 insert 했던 코드에는 BeanCreationException 에러가 발생합니다.

 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement


BeanCreationException이란?

Exception thrown when a BeanFactory encounters an error when attempting to create a bean from a bean definition.

 

bean definition에서 bean을 생성하려고 할때 BeanFactory가 error를 만나면 발생하는 예외입니다.

그 이유는 Spring Boot 2.5 Release Notes의 Hibernate and data.sql를 보면 확인할 수 있습니다.


Hibernate and data.sql

By default, data.sql scripts are now run before Hibernate is initialized.

기본적으로 data.sql 스크립트는 Hibernate가 초기화되기 전에 실행됩니다.

 

This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase.

기본 스크립트 기반 초기화(data.sql과 같은 스크립트 파일)의 동작을 Flyway와 Liquibase의 동작과 일치시킵니다.

 

If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true.

만약 Hibernate에 의해 생성된 스키마(DB 테이블)에 data.sql를 사용하려고 한다면(data.sql로 데이터를 insert 하려고 한다면) spring.jpa.defer-datasource-initialization를 true로 설정해야 합니다.

 

While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it’s populated via data.sql.

데이터 베이스 초기화 기술을 혼합하는 것은 권장 되지 않지만, data.sql가 실행되기 전에 Hibernate가 생성한 스키마를 기반으로 구축하는 것을 허용합니다.


해결방법

docs를 읽어본 결과 yml 파일에 아래의 설정을 추가하면 된다는 것을 알 수 있습니다.

 

spring:
    jpa:
        defer-datasource-initialization: true

'POST > Spring Boot' 카테고리의 다른 글

[책후기] 처음배우는 스프링부트2  (0) 2021.01.11