Spring Bootで Developer Tools と H2 Database を使っている時にハマったこと


Spring Bootのバージョン

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

POMの設定

  <!-- 略 -->
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!-- 略 -->
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>
    <!-- 略 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
       <optional>true</optional>
    </dependency>
  </dependencies>
  <!-- 略 -->

発生したエラー

2020-02-24 10:49:36.383  INFO 25790 --- [  restartedMain] com.example.App                          : No active profile set, falling back to default profiles: default
2020-02-24 10:49:36.406  INFO 25790 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-02-24 10:49:36.753  INFO 25790 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-02-24 10:49:36.859  INFO 25790 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-02-24 10:49:36.902  INFO 25790 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-02-24 10:49:36.978  INFO 25790 --- [  restartedMain] com.example.App                          : Started App in 0.781 seconds (JVM running for 1.329)
(略)
2020-02-24 10:49:37.213  WARN 25790 --- [extShutdownHook] o.s.b.f.support.DisposableBeanAdapter    : Invocation of destroy method failed on bean with name 'inMemoryDatabaseShutdownExecutor': org.h2.jdbc.JdbcSQLNonTransientConnectionException: データベースはすでに閉じられています (VM終了時の自動データベースクローズを無効にするためには、db URLに ";DB_CLOSE_ON_EXIT=FALSE" を追加してください)
Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-200]
2020-02-24 10:49:37.215  INFO 25790 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-02-24 10:49:37.222  INFO 25790 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

対応方法

application.propertiesapplication.yml)にspring.autoconfigure.exclude=org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfigurationを追加する。