none
SubmitChanges after ChangeConflicts.ResolveAll() RRS feed

  • Question

  • I have seen a number of examples of ResolveAll being used followed by another SubmitChanges in the ChangeConflictException catch block, thus:
    try
    {
        ctx.SubmitChanges(ConflictMode.ContinueOnConflict);
    }
    catch (ChangeConflictException ex)
    {
        ctx.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges, true);
        ctx.SubmitChanges(ConflictMode.FailOnFirstConflict);
    }
    However there is still a risk of a race condition, so the SubmitChanges in the catch block could raise another ChangeConflictException. Would it not be better to retry the SubmitChanges outside the catch block?

    bool success = false;
    int tries = 0;
    ConflictMode MyConflictMode = ConflictMode.ContinueOnConflict;
    
    do
    {
        try
        {
            tries++;
            ctx.SubmitChanges(MyConflictMode);
            success = true;
        }
        catch (ChangeConflictException ex)
        {
            if (tries > 3)
            {
                throw;
            }
    
            ctx.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges, true);
            MyConflictMode = ConflictMode.FailOnFirstConflict;
        }
        catch (Exception ex)
        {
            throw;
        }
    } while (success == false);

    Thanks,
    John.
    Friday, January 15, 2010 1:31 PM

Answers

  • Hello John,

    Generally, since the confliction exception has been raised, it is not recommended to call SubmitChanges() again in the catch block.  Of course, as you said, it is possible that the ResolveAll raises another confliction exception, but in my opinion, we just need to check the confliction on the ResolveAll method only once. 

    Have a nice day!


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 20, 2010 8:07 AM
    Moderator

All replies

  • Hello John,

    Generally, since the confliction exception has been raised, it is not recommended to call SubmitChanges() again in the catch block.  Of course, as you said, it is possible that the ResolveAll raises another confliction exception, but in my opinion, we just need to check the confliction on the ResolveAll method only once. 

    Have a nice day!


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 20, 2010 8:07 AM
    Moderator
  • That's great, thanks.

    Monday, January 25, 2010 10:35 AM