Are there delays between db.SaveChanges() returning, and actual writes being performed in the database?

Viewed 39

We have a stateless web application that runs some business logic automation. The logic, is powered by a state machine, and the state is stored in a SQL database in Azure.

We're having an issue where a user is attempting to override the state machine, and end the process, but rarely the state machine will keep running as if there was no user intervention.

public static void setStatus(Callout co, testdb2Entities5 db, string status, Boolean manualOverride)
    {

        try
        {
            if (manualOverride && !co.status.Contains(CalloutStatus.cancelled))
            {
                co.status = status;
            }
            else if(!(co.status.Contains(CalloutStatus.cancelled)
            || co.status.Contains(CalloutStatus.stopped)
            || co.status.Contains(CalloutStatus.confirmationNeeded)
            || co.status.Contains(CalloutStatus.finishedSucceeded)
            || co.status.Contains(CalloutStatus.awaitingApproval)))
            {
                Debug.WriteLine("Modifiy status to " + status + " for callout " + co.callout_id_pk);
                co.status = status;
                db.SaveChanges();
            }
            else
                Debug.WriteLine("Don't modify the status for callout " + co.callout_id_pk);
        }
        catch (SqlException e)
        {
            Debug.WriteLine(e.ToString());
            setStatus(co, db, status, manualOverride);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }
    }

I suspect that what's happening is that our code that modifies the state is being run to completion, the write to database hasn't actually completed, and then the state machine continues and performs a supplemental write that overrides the user authoritative action. This would explain why the issue only occurs rarely, as in most scenarios the code would be at a spot in its flow where this isn't possible (keep in mind this is a multithreaded application).

I need to understand the behavior of db.SaveChanges (or db.SaveChangesAsync) behaves, such that I can consider solutions to this scenario.

0 Answers
Related