Reputation: 900
I am using the hibernate with PostgreSQL database.I am using the PostgreSQL binary distribution and initialize,start and stop database manually.Is there any way in Java, hibernate to find out the database is not started ?
Upvotes: 1
Views: 679
Reputation: 2184
I don't know about PostgreSQL but should be same.
You can check the db availability be using Select 1
query.
@Query(nativeQuery=true, value="select 1")
public BigInteger getHeartBeat();
Fire the query occasionally to check the availability of database.
try {
BigInteger heartBeat = customerRepository.getHeartBeat();
if (heartBeat != null && 1 == heartBeat.intValue()) {
return "All ok";
}
} catch (Throwable e) {
return "Database not reachable";
}
Ideally if you have a web app, you can expose this service through a controller and configure in uptime robot. So if your service is down you get a notification.
Upvotes: 1