Reputation: 1334
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.
Upvotes: 6
Views: 10311
Reputation: 3355
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.
Upvotes: 8