예제 #1
0
        private async Task OpenWithCheckAndNotificationAsync(string checkQueryString,
                                                             DbConnectionAccessorExtension connectionAccessorEx, CancellationToken cancellationToken)
        {
            var connectionChecked = false;
            var restoreTriggered  = false;
            var accessors         = connectionAccessorEx.Accessors;

            while (!connectionChecked)
            {
                cancellationToken.ThrowIfCancellationRequested();

                await SqlHelper.NotifyConnectionOpeningAsync(accessors,
                                                             UnderlyingConnection, (!connectionChecked && !restoreTriggered), cancellationToken)
                .ConfigureAwait(false);

                await underlyingConnection.OpenAsync(cancellationToken).ConfigureAwait(false);

                try {
                    await SqlHelper.NotifyConnectionInitializingAsync(accessors,
                                                                      UnderlyingConnection, checkQueryString, (!connectionChecked && !restoreTriggered), cancellationToken)
                    .ConfigureAwait(false);

                    var command = underlyingConnection.CreateCommand();
                    await using (command.ConfigureAwait(false)) {
                        command.CommandText = checkQueryString;
                        _ = await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                    }
                    connectionChecked = true;
                    await SqlHelper.NotifyConnectionOpenedAsync(accessors, UnderlyingConnection, (!connectionChecked && !restoreTriggered), cancellationToken)
                    .ConfigureAwait(false);
                }
                catch (Exception exception) {
                    await SqlHelper.NotifyConnectionOpeningFailedAsync(accessors,
                                                                       UnderlyingConnection, exception, (!connectionChecked && !restoreTriggered), cancellationToken)
                    .ConfigureAwait(false);

                    if (InternalHelpers.ShouldRetryOn(exception))
                    {
                        if (restoreTriggered)
                        {
                            throw;
                        }
                        var newConnection = new SqlServerConnection(underlyingConnection.ConnectionString);
                        try {
                            underlyingConnection.Close();
                            underlyingConnection.Dispose();
                        }
                        catch { }

                        underlyingConnection = newConnection;
                        restoreTriggered     = true;
                        continue;
                    }

                    throw;
                }
            }
        }
예제 #2
0
        private void OpenWithCheckAndNotification(string checkQueryString, DbConnectionAccessorExtension connectionAccessorEx)
        {
            var connectionChecked = false;
            var restoreTriggered  = false;
            var accessors         = connectionAccessorEx.Accessors;

            while (!connectionChecked)
            {
                SqlHelper.NotifyConnectionOpening(accessors, UnderlyingConnection, (!connectionChecked && !restoreTriggered));
                underlyingConnection.Open();
                try {
                    SqlHelper.NotifyConnectionInitializing(accessors, UnderlyingConnection, checkQueryString, (!connectionChecked && !restoreTriggered));
                    using (var command = underlyingConnection.CreateCommand()) {
                        command.CommandText = checkQueryString;
                        _ = command.ExecuteNonQuery();
                    }
                    connectionChecked = true;
                    SqlHelper.NotifyConnectionOpened(accessors, UnderlyingConnection, (!connectionChecked && !restoreTriggered));
                }
                catch (Exception exception) {
                    SqlHelper.NotifyConnectionOpeningFailed(accessors, UnderlyingConnection, exception, (!connectionChecked && !restoreTriggered));
                    if (InternalHelpers.ShouldRetryOn(exception))
                    {
                        if (restoreTriggered)
                        {
                            throw;
                        }

                        var newConnection = new SqlServerConnection(underlyingConnection.ConnectionString);
                        try {
                            underlyingConnection.Close();
                            underlyingConnection.Dispose();
                        }
                        catch { }

                        underlyingConnection = newConnection;
                        restoreTriggered     = true;
                        continue;
                    }

                    throw;
                }
            }
        }
예제 #3
0
        private async Task OpenWithCheckAsync(string checkQueryString, CancellationToken cancellationToken)
        {
            bool connectionChecked = false;
            bool restoreTriggered  = false;

            while (!connectionChecked)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await base.OpenAsync(cancellationToken).ConfigureAwait(false);

                try {
                    using (var command = underlyingConnection.CreateCommand()) {
                        command.CommandText = checkQueryString;
                        await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                    }
                    connectionChecked = true;
                }
                catch (Exception exception) {
                    if (InternalHelpers.ShouldRetryOn(exception))
                    {
                        if (restoreTriggered)
                        {
                            throw;
                        }
                        var newConnection = new SqlServerConnection(underlyingConnection.ConnectionString);
                        try {
                            underlyingConnection.Close();
                            underlyingConnection.Dispose();
                        }
                        catch { }

                        underlyingConnection = newConnection;
                        restoreTriggered     = true;
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
예제 #4
0
        private void OpenWithCheck(string checkQueryString)
        {
            bool connectionChecked = false;
            bool restoreTriggered  = false;

            while (!connectionChecked)
            {
                base.Open();
                try {
                    using (var command = underlyingConnection.CreateCommand()) {
                        command.CommandText = checkQueryString;
                        command.ExecuteNonQuery();
                    }
                    connectionChecked = true;
                }
                catch (Exception exception) {
                    if (InternalHelpers.ShouldRetryOn(exception))
                    {
                        if (restoreTriggered)
                        {
                            throw;
                        }

                        var newConnection = new SqlServerConnection(underlyingConnection.ConnectionString);
                        try {
                            underlyingConnection.Close();
                            underlyingConnection.Dispose();
                        }
                        catch { }

                        underlyingConnection = newConnection;
                        restoreTriggered     = true;
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
 private void EnsureConnectionIsAlive(ref SqlServerConnection connection, string query)
 {
     try {
         using (var command = connection.CreateCommand()) {
             command.CommandText = query;
             command.ExecuteNonQuery();
         }
     }
     catch (Exception exception) {
         if (InternalHelpers.ShouldRetryOn(exception))
         {
             if (!TryReconnect(ref connection, query))
             {
                 throw;
             }
         }
         else
         {
             throw;
         }
     }
 }