mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user