Spring Boot应用程序启动速度非常慢

时间:2021-12-08 05:49:55

I have a simple Spring Boot application that connects to a PostgreSQL database and serves as a JSON service. Somehow the startup has become very slow, see timings 10:37:10 and 10:38:00:

我有一个简单的Spring Boot应用程序,它连接到PostgreSQL数据库并充当JSON服务。不知怎的,创业公司变得非常慢,见时间10:37:10和10:38:00:

2015-05-09 10:37:09.649  INFO 20880 --- [lication.main()] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-05-09 10:37:09.651  INFO 20880 --- [lication.main()] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.20
2015-05-09 10:37:09.767  INFO 20880 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-05-09 10:37:09.767  INFO 20880 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2970 ms
2015-05-09 10:37:09.979  INFO 20880 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-05-09 10:37:09.985  INFO 20880 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-05-09 10:37:10.105  INFO 20880 --- [lication.main()] o.s.j.d.DriverManagerDataSource          : Loaded JDBC driver: org.postgresql.Driver
2015-05-09 10:37:10.214  INFO 20880 --- [lication.main()] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2015-05-09 10:37:10.233  INFO 20880 --- [lication.main()] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2015-05-09 10:37:10.585  INFO 20880 --- [lication.main()] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.8.Final}
2015-05-09 10:37:10.587  INFO 20880 --- [lication.main()] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2015-05-09 10:37:10.589  INFO 20880 --- [lication.main()] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2015-05-09 10:37:10.968  INFO 20880 --- [lication.main()] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2015-05-09 10:38:00.023  INFO 20880 --- [lication.main()] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2015-05-09 10:38:00.041  INFO 20880 --- [lication.main()] o.h.e.jdbc.internal.LobCreatorBuilder    : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2015-05-09 10:38:00.274  INFO 20880 --- [lication.main()] o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory

Any thoughts? Is there anything I can do to diagnose the problem?

有什么想法吗?我能做些什么来诊断问题吗?

6 个解决方案

#1


For Spring Boot you can set this in your application.properties file:

对于Spring Boot,您可以在application.properties文件中进行设置:

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

I also found that I needed to set another property or I would get the error "org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set". To rectify that I set this property:

我还发现我需要设置另一个属性,否则我会收到错误“org.hibernate.HibernateException:当'hibernate.dialect'没有设置”时,对DialectResolutionInfo的访问不能为空。为了纠正我设置此属性:

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

This reduced our startup time from about 100 seconds down to 12.

这将我们的启动时间从大约100秒减少到12秒。

#2


Problem solved using

用问题解决了

properties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");

Thanks all.

#3


Contributing application.yml version of property setting.

贡献application.yml版本的属性设置。

spring:
  jpa:
    properties:
      hibernate:
        temp:
          use_jdbc_metadata_defaults: false

#4


For dev environment, use the following property

对于dev环境,请使用以下属性

spring.jpa.hibernate.ddl-auto=none

This would be a bit risky for staging and prod environment.

这对于升级和生产环境来说会有点风险。

#5


Are you running the tests on a local server? Perhaps there's some problem with the database server URL, such as a non-resolvable hostname or an IPv6 DNS entry, connecting with the same connection string from another application (e.g. http://squirrel-sql.sourceforge.net/) could confirm the problem.

您是否在本地服务器上运行测试?也许数据库服务器URL存在一些问题,例如不可解析的主机名或IPv6 DNS条目,使用来自其他应用程序的相同连接字符串(例如http://squirrel-sql.sourceforge.net/)连接可以确认问题。

The delay is definitely logged when creating the database connection for the first time (either while loading the driver or performing the connection).

首次创建数据库连接时(无论是在加载驱动程序还是执行连接时),都会记录延迟。

#6


I found the startup takes long time when the db server is far, in my case it won't take time when using the localhost db, and it take about 20 seconds in product enviroment with db is in us and server is in jp.

我发现启动需要很长时间,因为数据库服务器很远,在我的情况下,使用localhost数据库时不需要时间,并且在产品环境中需要大约20秒,而db在我们中,而服务器在jp中。

#1


For Spring Boot you can set this in your application.properties file:

对于Spring Boot,您可以在application.properties文件中进行设置:

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

I also found that I needed to set another property or I would get the error "org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set". To rectify that I set this property:

我还发现我需要设置另一个属性,否则我会收到错误“org.hibernate.HibernateException:当'hibernate.dialect'没有设置”时,对DialectResolutionInfo的访问不能为空。为了纠正我设置此属性:

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

This reduced our startup time from about 100 seconds down to 12.

这将我们的启动时间从大约100秒减少到12秒。

#2


Problem solved using

用问题解决了

properties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");

Thanks all.

#3


Contributing application.yml version of property setting.

贡献application.yml版本的属性设置。

spring:
  jpa:
    properties:
      hibernate:
        temp:
          use_jdbc_metadata_defaults: false

#4


For dev environment, use the following property

对于dev环境,请使用以下属性

spring.jpa.hibernate.ddl-auto=none

This would be a bit risky for staging and prod environment.

这对于升级和生产环境来说会有点风险。

#5


Are you running the tests on a local server? Perhaps there's some problem with the database server URL, such as a non-resolvable hostname or an IPv6 DNS entry, connecting with the same connection string from another application (e.g. http://squirrel-sql.sourceforge.net/) could confirm the problem.

您是否在本地服务器上运行测试?也许数据库服务器URL存在一些问题,例如不可解析的主机名或IPv6 DNS条目,使用来自其他应用程序的相同连接字符串(例如http://squirrel-sql.sourceforge.net/)连接可以确认问题。

The delay is definitely logged when creating the database connection for the first time (either while loading the driver or performing the connection).

首次创建数据库连接时(无论是在加载驱动程序还是执行连接时),都会记录延迟。

#6


I found the startup takes long time when the db server is far, in my case it won't take time when using the localhost db, and it take about 20 seconds in product enviroment with db is in us and server is in jp.

我发现启动需要很长时间,因为数据库服务器很远,在我的情况下,使用localhost数据库时不需要时间,并且在产品环境中需要大约20秒,而db在我们中,而服务器在jp中。