Spring Boot Actuator Database Health Check - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here I am going to show how to check database health using Spring boot actuator health endpoint.
Include the spring boot actuator dependency in pom.xml
<dependency>
  <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
We can do this check in two different solutions.
Enabling the management config property in application.properties/yml file.
management.endpoints.health.sensitive=false
management.health.db.enabled=true
management.health.defaults.enabled=true
management.endpoint.health.show-details=always
 
Run the Application and access actuator health endpoint.
http://localhost:8080/actuator/health
Creating the custom Actuator service.
Creating DbHealthCheck class implementing HealthIndicator and override health() method.
Using JdbcTemplate, execute the sample SQL query to check whether the database connected or not.
package com.onlinetutorialspoint.actuator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DbHealthCheck implements HealthIndicator {
    @Autowired
    JdbcTemplate template;
    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 1) {
            return Health.down().withDetail("Error Code", 500).build();
        }
        return Health.up().build();
    }

    public int check(){
        List<Object> results = template.query("select 1 from dual",
                new SingleColumnRowMapper<>());
        return results.size();
    }
}

Run the application.
http://localhost:8080/actuator/health
References:
Spring Boot Production ready endpoints
Spring Boot Actuator
Happy Learning 🙂

Spring Boot EhCache Example
How to Get All Spring Beans Details Loaded in ICO
Spring Boot Hazelcast Cache Example
Spring Boot JNDI Configuration – External Tomcat
Simple Spring Boot Example
Spring Boot How to change the Tomcat to Jetty Server
Spring Boot Hibernate Integration Example
How to Send Mail Spring Boot Example
Spring Boot Apache ActiveMq In Memory Example
Spring Boot JPA Integration Example
Spring Boot Actuator Example
Spring Boot Batch Example Csv to Database
Spring Boot H2 Database + JDBC Template Example
Spring Boot Security MySQL Database Integration Example
Spring Boot MongoDB + Spring Data Example

Spring Boot EhCache Example
How to Get All Spring Beans Details Loaded in ICO
Spring Boot Hazelcast Cache Example
Spring Boot JNDI Configuration – External Tomcat
Simple Spring Boot Example
Spring Boot How to change the Tomcat to Jetty Server
Spring Boot Hibernate Integration Example
How to Send Mail Spring Boot Example
Spring Boot Apache ActiveMq In Memory Example
Spring Boot JPA Integration Example
Spring Boot Actuator Example
Spring Boot Batch Example Csv to Database
Spring Boot H2 Database + JDBC Template Example
Spring Boot Security MySQL Database Integration Example
Spring Boot MongoDB + Spring Data Example






ravi
May 25, 2020 at 1:43 pm - Reply 

where is the controller , datapase details of properties file










ravi
May 25, 2020 at 1:43 pm - Reply 

where is the controller , datapase details of properties file




where is the controller , datapase details of properties file
 


Δ
 Spring Boot – Hello World
 Spring Boot – MVC Example
 Spring Boot- Change Context Path
 Spring Boot – Change Tomcat Port Number
 Spring Boot – Change Tomcat to Jetty Server
 Spring Boot – Tomcat session timeout
 Spring Boot – Enable Random Port
 Spring Boot – Properties File
 Spring Boot – Beans Lazy Loading
 Spring Boot – Set Favicon image
 Spring Boot – Set Custom Banner
 Spring Boot – Set Application TimeZone
 Spring Boot – Send Mail
 Spring Boot – FileUpload Ajax
 Spring Boot – Actuator
 Spring Boot – Actuator Database Health Check
 Spring Boot – Swagger
 Spring Boot – Enable CORS
 Spring Boot – External Apache ActiveMQ Setup
 Spring Boot – Inmemory Apache ActiveMq
 Spring Boot – Scheduler Job
 Spring Boot – Exception Handling
 Spring Boot – Hibernate CRUD
 Spring Boot – JPA Integration CRUD
 Spring Boot – JPA DataRest CRUD
 Spring Boot – JdbcTemplate CRUD
 Spring Boot – Multiple Data Sources Config
 Spring Boot – JNDI Configuration
 Spring Boot – H2 Database CRUD
 Spring Boot – MongoDB CRUD
 Spring Boot – Redis Data CRUD
 Spring Boot – MVC Login Form Validation
 Spring Boot – Custom Error Pages
 Spring Boot – iText PDF
 Spring Boot – Enable SSL (HTTPs)
 Spring Boot – Basic Authentication
 Spring Boot – In Memory Basic Authentication
 Spring Boot – Security MySQL Database Integration
 Spring Boot – Redis Cache – Redis Server
 Spring Boot – Hazelcast Cache
 Spring Boot – EhCache
 Spring Boot – Kafka Producer
 Spring Boot – Kafka Consumer
 Spring Boot – Kafka JSON Message to Kafka Topic
 Spring Boot – RabbitMQ Publisher
 Spring Boot – RabbitMQ Consumer
 Spring Boot – SOAP Consumer
 Spring Boot – Soap WebServices
 Spring Boot – Batch Csv to Database
 Spring Boot – Eureka Server
 Spring Boot – MockMvc JUnit
 Spring Boot – Docker Deployment