Adding async method (#235)

Adding an OpenAsync method to the ReliableConnection to allow it to properly utilize the cancellation token support of the SqlConnection. It eliminates the need for a "cancellation thread" that has a high likelihood of throwing uncaught exceptions (as per Microsoft/vscode-mssql/issues/547)
This commit is contained in:
Benjamin Russell
2017-02-17 16:26:24 -08:00
committed by GitHub
parent 2490be2c8a
commit fda0005241

View File

@@ -31,6 +31,8 @@ using System.Data.SqlClient;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection
@@ -260,7 +262,47 @@ SET NUMERIC_ROUNDABORT OFF;";
/// </summary>
public override void Open()
{
OpenConnection();
// Check if retry policy was specified, if not, disable retries by executing the Open method using RetryPolicy.NoRetry.
_connectionRetryPolicy.ExecuteAction(() =>
{
if (_underlyingConnection.State != ConnectionState.Open)
{
_underlyingConnection.Open();
}
SetLockAndCommandTimeout(_underlyingConnection);
SetDefaultAnsiSettings(_underlyingConnection, IsSqlDwConnection(_underlyingConnection));
});
}
/// <summary>
/// Opens a database connection with the settings specified by the ConnectionString
/// property of the provider-specific Connection object.
/// </summary>
public override Task OpenAsync(CancellationToken token)
{
// Make sure that the token isn't cancelled before we try
if (token.IsCancellationRequested)
{
return Task.FromCanceled(token);
}
// Check if retry policy was specified, if not, disable retries by executing the Open method using RetryPolicy.NoRetry.
try
{
return _connectionRetryPolicy.ExecuteAction(async () =>
{
if (_underlyingConnection.State != ConnectionState.Open)
{
await _underlyingConnection.OpenAsync(token);
}
SetLockAndCommandTimeout(_underlyingConnection);
SetDefaultAnsiSettings(_underlyingConnection, IsSqlDwConnection(_underlyingConnection));
});
}
catch (Exception e)
{
return Task.FromException(e);
}
}
/// <summary>
@@ -356,26 +398,6 @@ SET NUMERIC_ROUNDABORT OFF;";
RetryPolicyUtils.RaiseSchemaAmbientRetryMessage(retryState, SqlSchemaModelErrorCodes.ServiceActions.ConnectionRetry, _azureSessionId);
}
/// <summary>
/// Opens a database connection with the settings specified by the ConnectionString and ConnectionRetryPolicy properties.
/// </summary>
/// <returns>An object representing the open connection.</returns>
private SqlConnection OpenConnection()
{
// Check if retry policy was specified, if not, disable retries by executing the Open method using RetryPolicy.NoRetry.
_connectionRetryPolicy.ExecuteAction(() =>
{
if (_underlyingConnection.State != ConnectionState.Open)
{
_underlyingConnection.Open();
}
SetLockAndCommandTimeout(_underlyingConnection);
SetDefaultAnsiSettings(_underlyingConnection, IsSqlDwConnection(_underlyingConnection));
});
return _underlyingConnection;
}
public void OnConnectionStateChange(object sender, StateChangeEventArgs e)
{
SqlConnection conn = (SqlConnection)sender;