简体   繁体   English

如何处理网络故障代码并恢复原状?

[英]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){

NetworkProblem = false; 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 //步骤1-从数据库中读取信息//步骤2-存储在类对象上//步骤3-更新信息//步骤4-连接到另一个Web服务器//步骤5-更新信息

}catch(NetworkgoneException){ NetworkProblem = true; } 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). 对于奖励积分,请区分值得重试的错误和不值得重试的错误(例如,权威的否定DNS响应)。
  • 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? 您是否需要某种交易概念来避免触发重复操作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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