Files
sqltoolsservice/src/Microsoft.SqlTools.ServiceLayer/Connection/ReliableConnection/RetryPolicy.IErrorDetectionStrategy.cs
Mitchell Sternke f2a5654a20 Feature/reliable connection (#44)
* Initial commit of reliable connection port

* Made ReliableSqlConnection inherit from DbConnection instead of IDbConnection

* Cleanup

* Fixed autocomplete service to use reliable connection

* Fix copyright headers

* Renamed ConnectResponse.Server to ServerInfo

* Removed unused using

* Addressing code review feedback
2016-09-13 18:10:26 -07:00

98 lines
3.8 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Data.SqlClient;
namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection
{
internal abstract partial class RetryPolicy
{
public interface IErrorDetectionStrategy
{
/// <summary>
/// Determines whether the specified exception represents a temporary failure that can be compensated by a retry.
/// </summary>
/// <param name="ex">The exception object to be verified.</param>
/// <returns>True if the specified exception is considered as temporary, otherwise false.</returns>
bool CanRetry(Exception ex);
/// <summary>
/// Determines whether the specified exception can be ignored.
/// </summary>
/// <param name="ex">The exception object to be verified.</param>
/// <returns>True if the specified exception is considered as non-harmful.</returns>
bool ShouldIgnoreError(Exception ex);
}
/// <summary>
/// Base class with common retry logic. The core behavior for retrying non SqlExceptions is the same
/// across retry policies
/// </summary>
internal abstract class ErrorDetectionStrategyBase : IErrorDetectionStrategy
{
public bool CanRetry(Exception ex)
{
if (ex != null)
{
SqlException sqlException;
if ((sqlException = ex as SqlException) != null)
{
return CanRetrySqlException(sqlException);
}
if (ex is InvalidOperationException)
{
// Operations can throw this exception if the connection is killed before the write starts to the server
// However if there's an inner SqlException it may be a CLR load failure or other non-transient error
if (ex.InnerException != null
&& ex.InnerException is SqlException)
{
return CanRetry(ex.InnerException);
}
return true;
}
if (ex is TimeoutException)
{
return true;
}
}
return false;
}
public bool ShouldIgnoreError(Exception ex)
{
if (ex != null)
{
SqlException sqlException;
if ((sqlException = ex as SqlException) != null)
{
return ShouldIgnoreSqlException(sqlException);
}
if (ex is InvalidOperationException)
{
// Operations can throw this exception if the connection is killed before the write starts to the server
// However if there's an inner SqlException it may be a CLR load failure or other non-transient error
if (ex.InnerException != null
&& ex.InnerException is SqlException)
{
return ShouldIgnoreError(ex.InnerException);
}
}
}
return false;
}
protected virtual bool ShouldIgnoreSqlException(SqlException sqlException)
{
return false;
}
protected abstract bool CanRetrySqlException(SqlException sqlException);
}
}
}