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 a
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.
I used the following piece of code
spring:
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
show-sql: false
generate-ddl: true
**hibernate:
ddl-auto: none
properties:
hibernate.hbm2ddl.auto: none**
For Spring Boot you can set this in your application.properties file:
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:
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
This reduced our startup time from about 100 seconds down to 12.
When I point to database in AWS RDS server it takes 78 seconds. After given this configuration it takes 52 seconds.
spring:
jpa:
properties:
hibernate:
temp:
use_jdbc_metadata_defaults: false
And after given this configuration with the above configuration it takes only 10 seconds.
spring:
jpa:
hibernate:
ddl-auto: none
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.
The delay is definitely logged when creating the database connection for the first time (either while loading the driver or performing the connection).
Problem solved using
properties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
Thanks all.