I have an application which makes calls to an online database. If I lose the connection to the internet or the database is down my application will crash.
What is the approach to handle this?
My own approach would be to have a object which keeps calling the database all the time and if an exception is thrown, then an event is fired which all the other components register to and which then would disable some functions.
Is this the right way or is there a better solution?
Difficult to say what would be best for you with knowing the bigger picture, but I can think of 3 ways you could potentially hand these situations:
1) handles the connectivity issues well at the point in time they happen and just report them nicely
2) like you say, periodically poll for connectivity and disable functionality if the “ping” fails
3) work with a local copy of the db, then synch updates up to the main db when connectivity is there
IMO, I’d go for 1) or 3).
Option 3 gives the best user experience.
Option 1 potentially involves the least dev effort.
With option 2, you have the issue that no matter how often you poll, you could hit a connectivity issue at any time.