示例#1
0
        static internal void TryOpenConnection(IDbConnection connection)
        {
            // This is the retry loop, handling the retries session
            // is done in the catch for performance reasons
            // RetryAmount + 1 because it's initial attempt + RetryAmount (5)
            for (int attempt = 0; attempt < RetryAmount + 1; attempt++)
            {
                try
                {
                    if (attempt > 0)
                    {
                        SyncServiceTracer.TraceInfo("Retrying opening connection, attempt {0} of {1}.", attempt, RetryAmount);
                    }

                    // Open the connection
                    connection.Open();

                    // Test the open connection
                    SqlConnection sqlConn = connection as SqlConnection;
                    if (sqlConn != null)
                    {
                        using (SqlCommand sqlCommand = new SqlCommand("Select 1", sqlConn))
                        {
                            sqlCommand.ExecuteScalar();
                            // Connection is valid, return successful
                            return;
                        }
                    }
                    else
                    {
                        //Don't test the injected connection
                        return;
                    }
                }
                catch (SqlException sqlException)
                {
                    // Throw Error if we have reach the maximum number of retries
                    if (attempt == RetryAmount)
                    {
                        SyncServiceTracer.TraceError("Open connection failed after max retry attempts, due to exception: {0}", sqlException.Message);
                        throw;
                    }

                    // Determine if we should retry or abort.
                    if (!RetryLitmus(sqlException))
                    {
                        SyncServiceTracer.TraceError("Open connection failed on attempt {0} of {1}, due to unretryable exception: {2}",
                                                     attempt + 1, RetryAmount, sqlException.Message);
                        throw;
                    }
                    else
                    {
                        SyncServiceTracer.TraceWarning("Open connection failed on attempt {0} of {1}, due to retryable exception: {2}",
                                                       attempt + 1, RetryAmount, sqlException.Message);
                        // Backoff Throttling
                        Thread.Sleep(RetryWaitMilliseconds * (int)Math.Pow(2, attempt));
                    }
                }
            }
        }
示例#2
0
        private static void CleanupBatchDirectory(string path, string headerFilePath, BatchHeader header)
        {
            try
            {
                if (Directory.Exists(path))
                {
                    // First delete all the batch related files.
                    foreach (var batchFileName in header.BatchFileNames)
                    {
                        string filePath = Path.Combine(path, batchFileName);
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }
                    }

                    // Delete the header file.
                    if (File.Exists(headerFilePath))
                    {
                        File.Delete(headerFilePath);
                    }

                    // If there are no other files in the directory then delete the directory.
                    if (0 == Directory.GetFiles(path).Length)
                    {
                        Directory.Delete(path, true);
                    }
                }
            }
            catch (Exception exception)
            {
                SyncServiceTracer.TraceWarning("Error cleaning up batch directory. " + WebUtil.GetExceptionMessage(exception));
            }
        }