'Handling Deadlocks via, RESTFUL API?

Goal is to improve code.

The ASP.NET MVC client Application makes the HTTP request to the service Layer.

This is a C# REST API, where we are transferring large DataSets to another REST API, which is the Database Layer. The Database Repository does Inserts into Tables that are Normalized. Eg: New CustomerOrders, CustomerPayments, Charges, Orders etc.

Since we have a large DataSet, first option was to split them in small chunks, split by 1000 Customers in one Batch. Which is sent over in Batches.

  1. All methods in code implements async / await
  2. Set HttpClient Timeout to TimeSpan.FromMinutes(2.1)

We had a situation when the process failed before Inserting all the Batches. The error was a DEADLOCK. This occurs due to conflicts where one stored Procedure INSERT awaits another STORED PROCEDURE to be released. Since my inserts are in a circular locking chain. Eg: If I had 10 Batches. 9 Batches were inserted and the Last one failed to the SQL DEADLOCK error.

Please provide recommendations or suggestions to handle the large data transfer. int actualorderRequestsYetToProcess = 6 where my Batches are split

Would it be a good option to apply a retry mechanism if any of the requests fails to complete?

public class FirstService { private async Task> PutCustomerOrders(IEnumerable orderRequestList) { foreach (OrderRequest request in actualorderRequestsYetToProcess) {

        CustomerOrdersResponse orderResponse = null;
        using (HttpClient client = _httpClient.GetHttpClient(user, culture))
        {
            client.BaseAddress = new Uri(_ServiceURL + "/orderRequest");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.Timeout = TimeSpan.FromMinutes(2.1);

            var json = JsonConvert.SerializeObject(request);
            HttpContent content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

            HttpResponseMessage response = await client.PostAsync(client.BaseAddress, content);

            orderResponse = new CustomerOrdersResponse
            {
                SessionNumber = request.SessionNumber,
                SessionTotal = request.SessionTotal
            };

            if (response.IsSuccessStatusCode)
            {
                orderResponse.SuccessOrFailure = true;
            }
            else
            {
                var x = response.Content.ReadAsStringAsync().Result;
                if (x != null)
                {
                    orderResponse.Errors = JsonConvert.DeserializeObject<IEnumerable<Error>>(x).ToList();
                }

                orderResponse.SuccessOrFailure = false;
            }
        }
        customerorderResponses.Add(orderResponse);
    }
}
}
public class SecondService
{
private List<Error> orderRequest(OrderRequest NewRequest, ref Guid SessionId)
{
    PostOrders();
}
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source