|
| 1 | +**문제** |
| 2 | + |
| 3 | + |
| 4 | +```yaml |
| 5 | +2025-04-09 17:50:52 25-04-09 08:50:52.859 [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: |
| 6 | + org.springframework.beans.factory.BeanCreationException: |
| 7 | + Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: |
| 8 | + Failed to initialize dependency 'dataSourceScriptDatabaseInitializer' of LoadTimeWeaverAware bean 'entityManagerFactory': |
| 9 | + Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Failed to execute database script |
| 10 | + 2025-04-09 17:50:52 25-04-09 08:50:52.864 [main] INFO o.a.catalina.core.StandardService - Stopping service [Tomcat] |
| 11 | + 2025-04-09 17:50:52 25-04-09 08:50:52.901 [main] INFO o.s.b.a.l.ConditionEvaluationReportLogger - |
| 12 | + 2025-04-09 17:50:52 |
| 13 | + 2025-04-09 17:50:52 Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. |
| 14 | + 2025-04-09 17:50:52 25-04-09 08:50:52.937 [main] ERROR o.s.boot.SpringApplication - Application run failed |
| 15 | +2025-04-09 17:50:52 org.springframework.beans.factory.BeanCreationException: |
| 16 | + Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: |
| 17 | + Failed to initialize dependency 'dataSourceScriptDatabaseInitializer' of LoadTimeWeaverAware bean 'entityManagerFactory': |
| 18 | + Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Failed to execute database script |
| 19 | +``` |
| 20 | +
|
| 21 | +Docker 컨테이너에서 애플리케이션을 실행할 때 데이터베이스 초기화 스크립트 실행에 실패하면서 ApplicationContext 초기화에 실패(Error starting |
| 22 | +ApplicationContex)했다. |
| 23 | +
|
| 24 | +즉, Spring Boot 가 실행 시 데이터베이스 초기화를 위해 실행하는 SQL 스크립트에서 에러가 발생했기 때문에 `entityManagerFactory` 를 생성하지 못해 |
| 25 | +애플리케이션 자체의 실행이 불가능해진 상황이다. |
| 26 | + |
| 27 | +**문제가 일어나는 이유** |
| 28 | + |
| 29 | +<aside> |
| 30 | + |
| 31 | +- 처음에는 `SPRING_PROFILES_ACTIVE=prod` 를 docker-compose.yml 에 설정해주지 않아서 그런가 생각했다. |
| 32 | +- 실제로 입력해주니 다시 8081 포트의 애플리케이션이 뜨기도 했지만, |
| 33 | +- 또 다시 생각해보니, 이제 컨테이너에서 실행되고 있는 Postgres 를 DB로서 연동할 것인데, 이런 방식을 이용하면 `application-prod.yml` 에 지정된 로컬 |
| 34 | + DB 가 연동되는 것 아닌가? 생각했다. |
| 35 | + - → User 을 생성한 후, Postgres 컨테이너의 저장공간으로 지정해둔 Volume 을 확인했는데, User가 저장돼있다는 흔적이 없었다. |
| 36 | +- 이런 상황에는 어떻게 해야하는 것인가? 어디서 문제가 발생하고 있는가? |
| 37 | +- 중복 정의는 안 된다. 어떤 오류가 발생할지 모르니까. |
| 38 | + |
| 39 | +</aside> |
| 40 | + |
| 41 | +`application-prod.yml` 를 지정하지 않으면, `application.yml` 이 기본 설정값으로 설정된다. 이때, 확인해보니 |
| 42 | +`profiles: active: test` …로 지정되어 있었고, test 설정 파일에서는 H2 DB를 이용하고 있었다. |
| 43 | + |
| 44 | +- application.yml |
| 45 | + |
| 46 | + ```yaml |
| 47 | + spring: |
| 48 | + application: |
| 49 | + name: discodeit |
| 50 | + |
| 51 | + profiles: |
| 52 | + active: test |
| 53 | + |
| 54 | + jpa: |
| 55 | + show-sql: true |
| 56 | + |
| 57 | + servlet: |
| 58 | + multipart: |
| 59 | + max-file-size: 10MB # 파일 하나의 최대 크기 |
| 60 | + max-request-size: 20MB # 한 벙네 최대 업로드 가능 용량 |
| 61 | + |
| 62 | + discodeit: |
| 63 | + storage: |
| 64 | + type: local |
| 65 | + local: |
| 66 | + root-path: .discodeit/storage |
| 67 | + |
| 68 | + logging: |
| 69 | + level: |
| 70 | + root: INFO |
| 71 | + |
| 72 | + management: |
| 73 | + endpoints: |
| 74 | + web: |
| 75 | + exposure: |
| 76 | + include: |
| 77 | + - "health" |
| 78 | + - "info" |
| 79 | + - "metrics" |
| 80 | + - "loggers" |
| 81 | + endpoint: |
| 82 | + health: |
| 83 | + show-details: "ALWAYS" |
| 84 | + info: |
| 85 | + git: |
| 86 | + mode: full # Git 정보를 가져올 수 있도록 설정 |
| 87 | + |
| 88 | + info: |
| 89 | + app: |
| 90 | + name: Discodeit |
| 91 | + version: 1.7.0 |
| 92 | + java-version: 17 |
| 93 | + spring-boot-version: 3.4.0 |
| 94 | + |
| 95 | + datasource: |
| 96 | + url: jdbc:h2:mem:devDB;MODE=PostgreSQL |
| 97 | + driver-class-name: org.h2.Driver |
| 98 | + |
| 99 | + jpa: |
| 100 | + ddl-auto: validate |
| 101 | + |
| 102 | + storage: |
| 103 | + type: local |
| 104 | + local: |
| 105 | + root-path: .discodeit/storage |
| 106 | + |
| 107 | + multipart: |
| 108 | + max-file-size: 10MB |
| 109 | + max-request-size: 20MB |
| 110 | + |
| 111 | + springdoc: |
| 112 | + packages-to-scan: com.sprint.mission.discodeit.controller |
| 113 | + default-consumes-media-type: application/json;charset=UTF-8 |
| 114 | + default-produces-media-type: application/json;charset=UTF-8 |
| 115 | + swagger-ui: |
| 116 | + path: /swagger-ui.html |
| 117 | + disable-swagger-default-url: true |
| 118 | + display-request-duration: true |
| 119 | + operations-sorter: alpha |
| 120 | + ``` |
| 121 | + |
| 122 | +**해결** |
| 123 | + |
| 124 | +`application.yml` 에서 `profiles: active: test` 를 제거하고, `server : port : 80` 을 추가했다. |
| 125 | + |
| 126 | +그러자 컨테이너에서 실행 중인 PostgresSQL 과 안정적으로 연결및 테이블 생성이 완료되었다. |
0 commit comments