We do use Spring Boot Health Checks in our application. On one of the checked applications it looks like the DB cannot answer in a timely manner. We are using the DataSourceHealthIndicator, and this is answering after several seconds with an exception which is fine, but takes different timeframes to come back.
Could we set a timeout on this HealthIndicator (and most probably on others as well), so that the HealthIndicator reports back after say 2s in the latest with an error, if no connection to the DB can be established?
Or could we ask the Datasource if there are still any open connections available?
I know, that we should fix the problem with these connections and we are already working on this one, but a HealthCheck for something like this would be nice as well.
Spring Boot automatically provides a health indicator for standard components (like a DataSource ). The health check provided by a DataSource creates a connection to a database and performs a simple query, such as select 1 from dual to check that it is working.
You can enable or disable an actuator endpoint by setting the property management. endpoint. <id>. enabled to true or false (where id is the identifier for the endpoint).
For running the Spring Boot application, open the main application file, and run it as Java Application. When the application runs successfully, it shows the message in the console, as shown below. Now, open the browser and invoke the URL http://localhost:8080.
A Spring Boot Actuator Health Indicator provides health information of a Spring Boot Application. This health information is accessible at the /health endpoint and can be consumed by monitoring software. By default, multiple Health Indicators are auto-configured.
You could disable the default db health indicator in your application.properties
management.health.db.enabled=false
and implement custom HealthIndicator
that has desired behavior, as described here.
In your custom HealthIndicator
implemetation you could use a different JdbcTemplate
that will have desired timeout value of 2 seconds, something like this:
JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
jdbcTemplate.setQueryTimeout(2);
jdbcTemplate.execute(...);
If execute call throws an exception, your indicator should return Health.down()
, otherwise Health.up()
should be returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With