0
我们的应用程序使用EF 5和c#.net 4.5。由于我们的应用程序在世界各地都使用一台Cenral服务器,因此我们遇到了超时并失去与服务器的连接的问题。实体框架句柄连接丢失
目前我们捕获SaveChanges()方法并再次尝试,直到用户取消。我们可以为代码中的各种装载做什么?
编辑:我想提出的解决方案,但我不明白它的工作正确的:
public class MyRetryStrategy : ITransientErrorDetectionStrategy
{
public bool IsTransient(Exception ex)
{
if (ex != null && ex is SqlException)
{
foreach (SqlError error in (ex as SqlException).Errors)
{
switch (error.Number)
{
case 1205:
System.Diagnostics.Debug.WriteLine("SQL Error: Deadlock condition. Retrying...");
return true;
case -2:
System.Diagnostics.Debug.WriteLine("SQL Error: Timeout expired. Retrying...");
return true;
case -1:
System.Diagnostics.Debug.WriteLine("SQL Error: Timeout expired. Retrying...");
return true;
}
}
}
if (ex != null && ex is EntityException)
{
ex = ex.InnerException;
foreach (SqlError error in (ex as SqlException).Errors)
{
switch (error.Number)
{
case 1205:
System.Diagnostics.Debug.WriteLine("SQL Error: Deadlock condition. Retrying...");
return true;
case -2:
System.Diagnostics.Debug.WriteLine("SQL Error: Timeout expired. Retrying...");
return true;
case -1:
System.Diagnostics.Debug.WriteLine("SQL Error: Timeout expired. Retrying...");
return true;
}
}
}
// For all others, do not retry.
return false;
}
}
Enity框架只投我EntityExceptions所以我说第二个代码路径。
实际使用情况:
RetryPolicy policy = new RetryPolicy<MyRetryStrategy>(5, TimeSpan.FromMilliseconds(1000));
policy.ExecuteAction(()=>_context.ProductStatuses.Include(x => x.Name.Translations).Load());
在MyRetryStrategy的IsTransient方法不会被调用,也没有重试调用。我在测试之前测试过停止Databse。
我到底做错了什么?