简体   繁体   中英

How to handle code for network failure and resume back?

suppose am in the middle of updating some information and the network connection goes off.

How should i write my code to handle such situation and resume from where the code was last executing?

One possible approach i could think of is to catch the exeception. and redo the logic again.

While(NetworkProblem){

NetworkProblem = false; try{

//step 1 - Reading info from db //step 2 - storing on class objects //step 3 - updating info //step 4 - connecting to another webserver //step 5 - update info

}catch(NetworkgoneException){ NetworkProblem = true;
// try to connect again - this again i dont know how long will take. }

}

In principle your approach seems ok. Some points to consider:

  • You don't want to retry indefinitely. Have some maximum retry count, then abort.
  • For bonus points, distinguish errors that are worth retrying from those that are not (eg authoritative negative DNS response).
  • Think about what you are trying to accomplish in your communication. What if some data goes through and some does not? Do you need some kind of transaction concept to avoid triggering duplicat actions?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM