Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring HealthIndicator - Timeout

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.

like image 943
triplem Avatar asked Sep 02 '15 16:09

triplem


People also ask

How does spring actuator health check work?

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.

How do I turn on healthcheck in spring boot?

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).

How do I know if spring boot is running or not?

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.

What is health indicator in spring boot?

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.


1 Answers

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 JdbcTemplatethat 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.

like image 169
mzc Avatar answered Oct 12 '22 00:10

mzc