英文:
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 '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");
}
}
答案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-mode
as 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 {
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论