Spring Boot 2.3与JPA:CommandLineRunner完成延迟

huangapple go评论62阅读模式
英文:

Spring Boot 2.3 with JPA: CommandLineRunner finish delayed

问题

以下是翻译好的内容:

我在使用Spring Boot 2.3.3中的CommandLineRunner,使用JPA,在完成后有1分钟的延迟(见下面的日志)。即使没有实体类存在,也没有JPA操作,这种情况仍然会发生。即使只有一个主要为空的主类的项目也可以复现这个问题。

这在2.2.7版本中没有发生,但在2.3.1中发生。它不仅在h2上发生,在其他数据库(derby、oracle)上也发生。如果我只使用spring-boot-starter-data-jdbc而不是spring-boot-starter-data-jpa,就不会有延迟。

有没有什么办法可以避免这个延迟?

日志:

2020-09-01 10:51:57.408  INFO 30314 --- [           main] testcase.SlowShutdown                    : Starting SlowShutdown on io with PID 30314 
2020-09-01 10:51:57.410  INFO 30314 --- [           main] testcase.SlowShutdown                    : No active profile set, falling back to default profiles: default
2020-09-01 10:51:57.858  INFO 30314 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : 在DEFERRED模式下引导Spring Data JPA存储库。
2020-09-01 10:51:57.873  INFO 30314 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : 完成Spring Data存储库扫描,耗时10毫秒。找到0个JPA存储库接口。
2020-09-01 10:51:58.095  INFO 30314 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : 正在初始化ExecutorService 'applicationTaskExecutor'
2020-09-01 10:51:58.100  INFO 30314 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - 正在启动...
2020-09-01 10:51:58.171  INFO 30314 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - 启动完成。
2020-09-01 10:51:58.216  INFO 30314 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : 处理PersistenceUnitInfo [name: default]
2020-09-01 10:51:58.243  INFO 30314 --- [         task-1] org.hibernate.Version                    : Hibernate ORM核心版本5.4.20.Final
2020-09-01 10:51:58.274  INFO 30314 --- [           main] DeferredRepositoryInitializationListener : 触发延迟初始化Spring Data存储库...
2020-09-01 10:51:58.274  INFO 30314 --- [           main] DeferredRepositoryInitializationListener : Spring Data存储库已初始化!
2020-09-01 10:51:58.282  INFO 30314 --- [           main] testcase.SlowShutdown                    : 在1.118秒内启动了SlowShutdown(JVM运行时间为1.579秒)
2020-09-01 10:51:58.283  INFO 30314 --- [           main] testcase.SlowShutdown                    : 启动
2020-09-01 10:51:58.315  INFO 30314 --- [         task-1] o.hibernate.annotations.common.Version   : Hibernate Commons Annotations版本{5.1.0.Final}
2020-09-01 10:51:58.387  INFO 30314 --- [         task-1] org.hibernate.dialect.Dialect            : 使用方言:org.hibernate.dialect.H2Dialect
2020-09-01 10:51:58.498  INFO 30314 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : 使用JtaPlatform实现:[org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-09-01 10:51:58.502  INFO 30314 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : 为持久性单元'default'初始化了JPA EntityManagerFactory
2020-09-01 10:52:58.512  INFO 30314 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : 为持久性单元'default'关闭了JPA EntityManagerFactory
2020-09-01 10:52:58.513  INFO 30314 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : 作为SessionFactory关闭的一部分,开始延迟逐出模式数据
2020-09-01 10:52:58.519  INFO 30314 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : 正在关闭ExecutorService 'applicationTaskExecutor'
2020-09-01 10:52:58.521  INFO 30314 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - 启动关闭...
2020-09-01 10:52:58.529  INFO 30314 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - 关闭完成。

Pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.3.RELEASE</version>
    <relativePath/>
  </parent>
  <groupId>test</groupId>
  <artifactId>slowshutdown</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>slowshutdown</name>
  <description>slowshutdown</description>
  <properties>
    <java.version>11</java.version>
  </properties>
  <dependencies>
    <dependency>
      <
英文:

I have a CommandLineRunner on Spring Boot 2.3.3 using JPA which has a 1 minute delay after finishing (see log below). This happens even when there are no entity classes present and there is no JPA operation. It's reproducible with a project consisting of only a mainly empty main class.

This did not occur with version 2.2.7, but does on 2.3.1. It happens not only on h2 but also on other databases (derby, oracle). If I use only spring-boot-starter-data-jdbc instead of spring-boot-starter-data-jpa there is no delay.

Is there any way to avoid this delay?

2020-09-01 10:51:57.408  INFO 30314 --- [           main] testcase.SlowShutdown                    : Starting SlowShutdown on io with PID 30314 
2020-09-01 10:51:57.410  INFO 30314 --- [           main] testcase.SlowShutdown                    : No active profile set, falling back to default profiles: default
2020-09-01 10:51:57.858  INFO 30314 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-09-01 10:51:57.873  INFO 30314 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 10ms. Found 0 JPA repository interfaces.
2020-09-01 10:51:58.095  INFO 30314 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService &#39;applicationTaskExecutor&#39;
2020-09-01 10:51:58.100  INFO 30314 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-09-01 10:51:58.171  INFO 30314 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-09-01 10:51:58.216  INFO 30314 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-09-01 10:51:58.243  INFO 30314 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.20.Final
2020-09-01 10:51:58.274  INFO 30314 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-09-01 10:51:58.274  INFO 30314 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-09-01 10:51:58.282  INFO 30314 --- [           main] testcase.SlowShutdown                    : Started SlowShutdown in 1.118 seconds (JVM running for 1.579)
2020-09-01 10:51:58.283  INFO 30314 --- [           main] testcase.SlowShutdown                    : start
2020-09-01 10:51:58.315  INFO 30314 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-09-01 10:51:58.387  INFO 30314 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2020-09-01 10:51:58.498  INFO 30314 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-09-01 10:51:58.502  INFO 30314 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit &#39;default&#39;
2020-09-01 10:52:58.512  INFO 30314 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit &#39;default&#39;
2020-09-01 10:52:58.513  INFO 30314 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down&#39;
2020-09-01 10:52:58.519  INFO 30314 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService &#39;applicationTaskExecutor&#39;
2020-09-01 10:52:58.521  INFO 30314 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-09-01 10:52:58.529  INFO 30314 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Pom:

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
    &lt;relativePath/&gt;
  &lt;/parent&gt;
  &lt;groupId&gt;test&lt;/groupId&gt;
  &lt;artifactId&gt;slowshutdown&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;slowshutdown&lt;/name&gt;
  &lt;description&gt;slowshutdown&lt;/description&gt;
  &lt;properties&gt;
    &lt;java.version&gt;11&lt;/java.version&gt;
  &lt;/properties&gt;
  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.h2database&lt;/groupId&gt;
      &lt;artifactId&gt;h2&lt;/artifactId&gt;
      &lt;scope&gt;runtime&lt;/scope&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
&lt;/project&gt;

Main class:

package testcase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class SlowShutdown implements CommandLineRunner {
  private static final Log log = LogFactory.getLog(SlowShutdown.class);

  public static void main(String[] args) {
    SpringApplication.run(SlowShutdown.class, args);
  }

  @Override
  public void run(String... args) throws Exception {
    log.info(&quot;start&quot;);
  }
}

答案1

得分: 4

Spring Boot 2.3版本附带了Spring Data的新主要版本。

正如他们在文档中所指出的,他们已更改了JPA存储库的默认BootstrapMode

> 从#16230开始,JPA存储库的默认BootstrapMode现在为"deferred",以改善启动时间。您可以使用spring.data.jpa.repositories.bootstrap-mode=default配置属性恢复到新的默认值。

为了避免延迟,您只需创建一个application.properties文件,并按照指示定义属性spring.data.jpa.repositories.bootstrap-mode

spring.data.jpa.repositories.bootstrap-mode=default
英文:

Spring Boot 2.3 version ships with a new major release of Spring Data.

As they indicate in the documentation, they changed the default BootstrapMode for JPA repositories:

> As of #16230, the default BootstrapMode for JPA repositories is now "deferred" so as to improve startup time. You can revert that new default with the spring.data.jpa.repositories.bootstrap-mode=default configuration property.

To avoid the delay, you only need to create an application.properties file, and define the property spring.data.jpa.repositories.bootstrap-modeas indicated:

spring.data.jpa.repositories.bootstrap-mode=default

答案2

得分: 3

请尝试将 @EnableJpaRepositories 添加到您的类中。

@SpringBootApplication
@EnableJpaRepositories
public class SlowShutdown implements CommandLineRunner {
}
英文:

Please try to add @EnableJpaRepositories to your class.

@SpringBootApplication
@EnableJpaRepositories
public class SlowShutdown implements CommandLineRunner {
}

huangapple
  • 本文由 发表于 2020年9月1日 17:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63684579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定