locked
Need "last writer wins" example RRS feed

  • Question

  • The description of "what's new" in Sync Framework 2.0 includes support for "last writer wins" conflict resolution. Are there any examples around of how to do this, preferably with SQL Server peer-to-peer, C#?

    Thanks

    Wednesday, July 14, 2010 9:46 AM

All replies

  • Hi Charles,

    The last writer wins is a core sync framework feature and the Database Providers use their own conflict handling .

    More info about it here:

    http://msdn.microsoft.com/en-us/library/cc761628.aspx

    -Jesse

    • Proposed as answer by Jesse L - MSFT Thursday, July 15, 2010 3:26 AM
    Thursday, July 15, 2010 3:25 AM
  • Thanks Jesse,

    I was able to get it to work, but perhaps not in the best way.

    The example does not do "last writer wins" nor refer to it. I added my own "date_mod" columns to the appropriate tables, which are updated by trigger. The code fragment for enforcing "last writer wins" is:

     

            private void dbProvider_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)

            {

                DataTable conflictingRemoteChange = e.Conflict.RemoteChange;

                DataTable conflictingLocalChange = e.Conflict.LocalChange;

                if (e.Conflict.Type == DbConflictType.LocalUpdateRemoteUpdate)

                {

                    Object oLocalTime = conflictingLocalChange.Rows[0]["date_mod"];

                    Object oRemoteTime = conflictingRemoteChange.Rows[0]["date_mod"];

                    if (oLocalTime != null && oRemoteTime != null)

                    {

                        DateTime localTime = (DateTime)oLocalTime;

                        DateTime remoteTime = (DateTime)oRemoteTime;

                        if (DateTime.Compare(localTime, remoteTime) < 0)

                        {

                            e.Action = ApplyAction.RetryWithForceWrite;

                            return;

                        }

                    }

                }

                e.Action = ApplyAction.Continue;

                return;


    I noticed, however, that the table [Sync].[MyTable_tracking], generated by the SqlSyncScopeProvisioning object, already contains a field called last_change_datetime, and it is populated properly. This field, however, is not copied over to the data table that is provided to the DbApplyChangeFailedEventArgs argument. Is there any easy way to access it?

    Thanks,
    Charlie

    Monday, July 19, 2010 9:01 AM