方法對於我DAL模塊一個我有很多重複的管道的形狀:使用PostSharp重試對異常
while (retry)
{
...
try
{
...do something
retry = false;
}
catch (SqlException sqlEx)
{
// Retry only if -2 = Connection Time Out or 1205 = Deadlock
if (sqlEx.Number == -2 || sqlEx.Number == 1205)
{
..retry if attempt < max
}
..log and rethrow exception
}
}
和已經發現PostSharp最近我試圖用更換這些管道代碼屬性。
我原來的計劃是: - 延長OnMethodInvocationAspect和方法調用 時記得方法調用事件參數 - 實施IOnExceptionAspect和實施onException的檢查異常類型,如果需要重試使用方法調用事件參數從原來的對象調用,即:
[Serializable]
public sealed class RetryAttribute : OnMethodInvocationAspect, IOnExceptionAspect
{
[NonSerialized]
private MethodInvocationEventArgs m_initialInvocationEventArgs = null;
public override void OnInvocation(MethodInvocationEventArgs eventArgs)
{
if (m_initialInvocationEventArgs == null)
m_initialInvocationEventArgs = eventArgs;
base.OnInvocation(eventArgs);
}
public void OnException(MethodExecutionEventArgs eventArgs)
{
// check if retry is necessary
m_initialInvocationEventArgs.Proceed();
}
}
但OnInvocation方法不解僱了,一旦我已經添加IOnExceptionAspect ..
有誰知道我需要在這裏做什麼?或者也許有更適合我應該使用的方面?
感謝,
我要問的是,爲什麼你獲得這些例外(逾時死鎖),並重新設計了應用程式移除要求捕獲這些問題 - 在應用程序拋出另一種技術是不會提高設計\ implmentation – AwkwardCoder 2009-08-21 13:45:16
嗯,是的,通常你不應該有死鎖。 重試的典型用法是在樂觀併發事務中工作。所以你可以重試,但我會同意奧利在那一點上。 – 2009-08-21 14:24:54
超時和死鎖不會在我們的環境中經常發生,但是由於我們有很多不同的應用程序(全球有成千上萬的用戶使用了超過100個應用程序..)使用相同的數據庫,並且許多應用程序同時觸擊相同的表,過去我們遇到了僵局/超時的情況。 – theburningmonk 2009-08-21 14:28:51