简体   繁体   English

带有 JPA 的 Spring Boot 2.3:CommandLineRunner 完成延迟

[英]Spring Boot 2.3 with JPA: CommandLineRunner finish delayed

I have a CommandLineRunner on Spring Boot 2.3.3 using JPA which has a 1 minute delay after finishing (see log below).我在 Spring Boot 2.3.3 上使用 JPA 有一个 CommandLineRunner,它在完成后有 1 分钟的延迟(见下面的日志)。 This happens even when there are no entity classes present and there is no JPA operation.即使不存在实体类并且没有 JPA 操作,也会发生这种情况。 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.这在 2.2.7 版本中没有发生,但在 2.3.1 上发生。 It happens not only on h2 but also on other databases (derby, oracle).它不仅发生在 h2 上,还发生在其他数据库(derby、oracle)上。 If I use only spring-boot-starter-data-jdbc instead of spring-boot-starter-data-jpa there is no delay.如果我只使用 spring-boot-starter-data-jdbc 而不是 spring-boot-starter-data-jpa 没有延迟。

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 'applicationTaskExecutor'
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 'default'
2020-09-01 10:52:58.512  INFO 30314 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-09-01 10:52:58.513  INFO 30314 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2020-09-01 10:52:58.519  INFO 30314 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
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:波姆:

<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>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

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("start");
  }
}

Spring Boot 2.3 version ships with a new major release of Spring Data. Spring Boot 2.3 版本附带了 Spring Data 的新主要版本。

As they indicate in the documentation , they changed the default BootstrapMode for JPA repositories:正如他们在文档中指出的那样,他们更改了 JPA 存储库的默认BootstrapMode

As of #16230 , the default BootstrapMode for JPA repositories is now "deferred" so as to improve startup time.#16230 开始,JPA 存储库的默认BootstrapMode现在“延迟”以缩短启动时间。 You can revert that new default with the spring.data.jpa.repositories.bootstrap-mode=default configuration property.您可以使用spring.data.jpa.repositories.bootstrap-mode=default配置属性恢复新的默认spring.data.jpa.repositories.bootstrap-mode=default

To avoid the delay, you only need to create an application.properties file, and define the property spring.data.jpa.repositories.bootstrap-mode as indicated:为了避免延迟,您只需要创建一个application.properties文件,并定义属性spring.data.jpa.repositories.bootstrap-mode如下所示:

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

Please try to add @EnableJpaRepositories to your class.请尝试将@EnableJpaRepositories添加到您的课程中。

@SpringBootApplication
@EnableJpaRepositories
public class SlowShutdown implements CommandLineRunner {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM