예제 #1
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 (SqlHelper.ShouldRetryOn(exception))
                    {
                        if (restoreTriggered)
                        {
                            SqlLog.Error(exception, Strings.LogConnectionRestoreFailed);
                            throw;
                        }
                        SqlLog.Warning(exception, Strings.LogGivenConnectionIsCorruptedTryingToRestoreTheConnection);

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

                        underlyingConnection = newConnection;
                        restoreTriggered     = true;
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
예제 #2
0
        private static bool TryReconnect(ref SqlServerConnection connection, string query)
        {
            try {
                var newConnection = new SqlServerConnection(connection.ConnectionString);
                try {
                    connection.Close();
                    connection.Dispose();
                }
                catch { }

                connection = newConnection;
                connection.Open();
                using (var command = connection.CreateCommand()) {
                    command.CommandText = query;
                    command.ExecuteNonQuery();
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #3
0
 public void Dispose()
 {
     _context?.Dispose();
 }