I'm writing a java app that connects to a DB on a server I have on my home network. I want to be able to dump the data as xml, and use that when I have my laptop and no db connection is available....I've been writing some code like so:
but it takes a long time for the db connection attempt to time out, and sometimes (in eclipse) the app just bombs before it gets to the else block.
What's the best way to handle this, meaning attempt to connect to the db, look for an xml file if it's unavailable.
Edmund Castermund wrote:...is that a standard thing; try to connect to a db, and connect to another source if it times out? Is it bad design?
It is not standard, but as per your requirements. Lots of applications fail when their database is not available.
It is not bad design. You require it. Your proposal to connect to the second source if connecting to the first one fails is ok.
However, if you are going to call getConnection each time you want to write something, that will be:
- a performance issue: you will hit the timeout every time
- a data consistency issue: you have saved a part to XML, then you plug in your network cable. Next data storage will happen to the database, because getConnection succeeds.
Isn't it better to check and select source once at program start, and continue using the data source until program ends?