locked
Conflict Resolution Problem - How to Force Server Changes to the Client RRS feed

  • Question

  • Hi

    I have an occasionally connected application with SQLServer 2008 (using change tracking) and SQLServer CE as the client cache.  I'm using bidirectional syncing.  I just want to implement a "last change wins" when synchronizing with the server.  Here's what I've tried:

    Partial Public Class ClientDataServerSyncProvider
    
      Private Sub clientDataServerSyncProvider_ApplyChangeFailed(ByVal sender As Object, ByVal e As Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs) Handles Me.ApplyChangeFailed
        On Error GoTo errorHandler
    
        'For now all conflicts are handled the same way:
        '***LATEST CHANGE WINS***
    
        Dim serverChanges As DataTable = e.Conflict.ServerChange
        Dim clientChanges As DataTable = e.Conflict.ClientChange
        Dim i As Int16
        For i = 0 To e.Conflict.ClientChange.Rows.Count - 1
    
            If clientChanges.Rows(i).Item("LastEditDate") > serverChanges.Rows(i).Item("LastEditDate") Then
    
              'Client change is newer than the last edit on the server - client wins
              e.Action = Microsoft.Synchronization.Data.ApplyAction.RetryWithForceWrite
    
            Else 'Server change must be newer - Server wins
    
              e.Action = Microsoft.Synchronization.Data.ApplyAction.Continue
    
            End If
            e.Transaction.Commit()
          End If
        Next
    

    Retry with force write DOES update the server with the client's changes but when the server changes are newer, how do I force the server changes to overwrite the client?

    Thanks

     

    Friday, November 5, 2010 9:52 PM

All replies

  • I noticed the event handler you have is for the Remote Provider (Server), have you done the same on the local client provider? when the changes are downloaded from the server and applied to the client, it's the local client provider that is invoked.

     

    Saturday, November 6, 2010 1:34 PM
  • Hi.  Thank you for your reply.

     

    Yes I also have a handler for the clientSyncProvider but it is never raised.  It looks like this:

     

    Partial Public Class ClientDataClientSyncProvider
    
      Private Sub clientDataClientSyncProvider_ApplyChangeFailed(ByVal sender As Object, ByVal e As Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs) Handles Me.ApplyChangeFailed
    
        Dim serverChanges As DataTable = e.Conflict.ServerChange
        Dim clientChanges As DataTable = e.Conflict.ClientChange
        Dim clientLost As Boolean = False
    
        Dim i As Int16
        For i = 0 To e.Conflict.ClientChange.Rows.Count - 1
    
            If clientChanges.Rows(i).Item("LastEditDate") > serverChanges.Rows(i).Item("LastEditDate") Then
    
              'Client change is newer than the last edit on the server - client wins
              e.Action = Microsoft.Synchronization.Data.ApplyAction.RetryWithForceWrite
    
            Else 'Server change must be newer - Server wins
    
              e.Action = Microsoft.Synchronization.Data.ApplyAction.Continue
              
            End If
        Next
    
       End Sub
    
    End Class
    

    Monday, November 8, 2010 6:11 PM
  • A minor thing I've noticed - is the e.Transaction.Commit() line supposed to miss from the code snippet on the client?

    Monday, November 8, 2010 10:04 PM
  • a conflict will only be raised if both client row and server row were modified. is that the case for your scenario?

    if you're doing bidirectional change, then client changes are uploaded and applied to the server first. then Sync Fx will retrieve the changes in the server and apply the same to the client.

    its not like Sync Fx will select change rows in the client and compare it with change rows in the server.

    Tuesday, November 9, 2010 4:15 PM