예제 #1
0
 private static bool IsAzure(SqlServerConnection connection)
 {
     using (var command = connection.CreateCommand()) {
         command.CommandText = "SELECT @@VERSION";
         return(((string)command.ExecuteScalar()).Contains("Azure"));
     }
 }
예제 #2
0
 private void EnsureConnectionIsAlive(ref SqlServerConnection connection, string query)
 {
     try {
         using (var command = connection.CreateCommand()) {
             command.CommandText = query;
             command.ExecuteNonQuery();
         }
     }
     catch (Exception exception) {
         if (SqlHelper.ShouldRetryOn(exception))
         {
             SqlLog.Warning(exception, Strings.LogGivenConnectionIsCorruptedTryingToRestoreTheConnection);
             if (!TryReconnect(ref connection, query))
             {
                 SqlLog.Error(exception, Strings.LogConnectionRestoreFailed);
                 throw;
             }
             SqlLog.Info(Strings.LogConnectionSuccessfullyRestored);
         }
         else
         {
             throw;
         }
     }
 }
예제 #3
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;
                    }
                }
            }
        }
예제 #4
0
        private static ErrorMessageParser CreateMessageParser(SqlServerConnection connection)
        {
            bool isEnglish;

            using (var command = connection.CreateCommand()) {
                command.CommandText = "SELECT @@LANGID";
                isEnglish           = command.ExecuteScalar().ToString() == "0";
            }
            var templates = new Dictionary <int, string>();

            using (var command = connection.CreateCommand()) {
                command.CommandText = MessagesQuery;
                using (var reader = command.ExecuteReader())
                    while (reader.Read())
                    {
                        templates.Add(reader.GetInt32(0), reader.GetString(1));
                    }
            }
            return(new ErrorMessageParser(templates, isEnglish));
        }
예제 #5
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);
            }
        }