I would like to find out if my database is running or not from my java code. I am not sure what approach will work best. The most obvious one is to have a thread that periodically polls the database. Another approach would be to use Triggers. I am not well versed in triggers, but i know that you can fire the trigger for updates, inserts and deletes. But, is it possible to fire the trigger when the database is down? It makes no sense to me trigger would work when the database is down.
PS: The database we are using right now is oracle, but I believe the same solution can be applied to any database
Thanks
Triggers are typically server-side constructs; so to “fire a trigger” you would need to be able to connect to the DB in the first place, which would require it to be up, which doesn’t help.
What is your motivation for “checking if the server is up”? Does it crash that often, or are you trying to be extra-careful before executing a database call that the DB has not crashed or your network connection lost?
If it is the second reason, assuming you are using the Java Oracle Driver, you’ll just get an exception from the failed call.
Code your calls to catch those exceptions and re-attempt the connection for retrying the call. This way you don’t need to poll the database; you just respond to the (infrequent) event when the connection is dead.
If you try 5 or so times to reconnect and cannot, then you could notify the caller that the database is dead and send an email to your admin or something.
There are any number of strategies here, but the point being that if you are trying to ensure calls execute, just wait for the java driver to fail and tell you instead of polling the DB server on a separate thread. No need for that.