Suppose i have a spring-boot application that has starter-JPA dependencies. I need to know if spring checks the connection to the db at the startup?
For example, if i have mentioned a Database that i do not have access to (like below), will the application fail to start? If yes what would be the exception?
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.dialect=org.hibernate.dialect.PostgreSQL94Dialect
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.continue-on-error=true
CodePudding user response:
For the last version of Spring Boot 2.7.2, Spring Boot doesn't check a connection itself, but gets a connection to determine some properties. Also Hibernate needs a connection to determine some properties itself (controlled by spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults
).
Also if you have spring-boot-starter-actuator
as a dependency, a connection will be checked at the startup (controlled by management.health.db.enabled
).
It needs to specify all automatically determined properties explicitly to not get a connection during the startup (even spring.jpa.hibernate.ddlAuto
)
Add this to application.yaml
spring.jpa.hibernate.ddlAuto: "none"
spring.datasource.driver-class-name: org.postgresql.Driver
spring.jpa.database-platform: org.hibernate.dialect.PostgreSQL94Dialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults: false #Hibernate
# disable datasource check on startup
management.health.db.enabled: false #spring-boot-starter-actuator
By default Hikari is used as a connection pool. The connection pool gets all connections at the first attempt to get a connection. That's why it doesn't get the connections during the startup.
CodePudding user response:
Nothing much. Just execute your query. If the connection has died, either your JDBC driver will reconnect (if it supports it, and you enabled it in your connection string--most don't support it) or else you'll get an exception.
our best chance is to just perform a simple query against one table, e.g.:
select 1 from SOME_TABLE;