如何处理数据库异常/问题? [英] How to handle database exceptions/ issue?

查看:196
本文介绍了如何处理数据库异常/问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我我的C#应用​​程序之一,我已经写SQL连接code如下

I one of my c# application, i have written sql connection code as following

try 
{ 
   myConnection = new SqlConnection(m_resourceDB.GetResourceString(nSiteID,  ApplicationID.XClaim,(short)nResID ) ); 
   myConnection.open();
}

我要处理类似SQLSERVER数据库的未知问题下来,超时。

I want to handle unkown issue of sqlserver like database down, time out.

有关这方面,我虽然在所有的问题引入循环3次,用3分钟的睡眠循环之间,如果有那么我将退出循环

For this i though to introduce for loop 3 times with 3 minute sleep between loop and if at all problem is there then i will exit from loop

我不知道我虽然是正确与否?我想在此方面的专家意见?任何例子吗?

I don't know my though is right or not? I want some expert advice on this? Any example?

推荐答案

我会简单地说:在code,讨论到连接等的不要做一个睡眠/重试,除非该code已经异步的。如果顶层的要求 code要捕获异常,并成立了一个计时器(未休眠),然后重试,再细 - 但要避免的是像:

I would say simply: the code that talks to connections etc should not be doing a sleep/retry, unless that code is already asynchronous. If the top-level calling code wants to catch an exception and set up a timer (not a sleep) and retry, then fine - but what you want to avoid is things like:

var data = dal.GetInfo();

突然走3分钟。您可能摆脱它,如果它是一个异步/回调,而你的明确标榜,这种方法可能需要几分钟来执行。但即使这样,感觉就像一个舒展。如果你是在上升的应用程序逻辑,为什么不第一时间捕捉异常,告诉用户,并让用户再次点击该按钮,在未来的某个时候?

suddenly taking 3 minutes. You might get away with it if it is an async/callback, and you have clearly advertised that this method may take minutes to execute. But even that feels like a stretch. And if you are up at the application logic, why not just catch the exception the first time, tell the user, and let the user click the button again at some point in the future?

这篇关于如何处理数据库异常/问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆