MOBILE

Programming .NET Framework 3.5 : Using Data Synchronization Services (part 3) - Handling Concurrency Errors

2/27/2012 11:17:40 AM
2.4. Handling Concurrency Errors

As we have just seen, rows may fail to synchronize; changes made at either end might not be acceptable at the other end. Therefore, we add one last bit of functionality to our demo. For each row that fails to synchronize, we need to determine which row it was, where it was rejected (server or client), and why it was rejected.

The possible reasons for why a row might fail to synchronize are specified by Microsoft in the ConflictType enumerator; see Table 1.

Table 1. Synchronization Errors
ConflictTypeMeaning
ClientDeleteServerUpdateThe client deleted a row that the server updated
ClientInsertServerInsertThe client and server both inserted a row that has the same primary key value
ClientUpdateServerDeleteThe server deleted a row that the client updated
ClientUpdateServerUpdateThe client and the server updated the same row

Note that ClientDeleteServerDelete is not an error. Any scenario in which the client wanted the row deleted and the row is deleted is not an error. The fact that the row was already deleted, and may have had different values in it from what the client thought it had, does not matter.

We start with the SyncAgent object; the object whose Synchronize method executes and monitors the entire synchronization. The SyncAgent object holds references to two SyncProvider objects: one that it uses to communicate with the server and one that it uses to communicate with the client. In our application, one is a SQL Server 2005 provider and one is a SQL Server CE provider. During synchronization, whenever a row cannot be inserted, updated, or deleted, at either the client or the server, because of a currency conflict, the appropriate provider raises an ApplyChangeFailed event. To process conflicts, we need to handle that event in our application. To illustrate, we will detect failures that occur at the client; highly similar code would be used to detect server-side failures.

It is the SyncAgent’s LocalProvider property object that will raise the ApplyChangeFailed event. To make this event visible to IntelliSense, we need to cast the LocalProvider to the specific class of provider that will reside in the property at runtime; normally either SqlCeClientSyncProvider or DbServerClientSyncProvider. Thus, our event signup code is:

((SqlCeClientSyncProvider)
   (syncAgent.LocalProvider))
      .ApplyChangeFailed +=
         new EventHandler
            <ApplyChangeFailedEventArgs>
               (FormData_ApplyChangeFailed);

The actual handler is shown in Listing 5. The EventArgs that we are handed contains a Conflict object, which, in turn, contains the reason for the row being rejected and also the row itself. The Conflict.ConflictType property contains the reason, while the Conflict.ClientChange property contains an untyped data table that holds the rejected row.

Listing 5. The Synchronization Error Handler
void FormData_ApplyChangeFailed(
              object sender,
              ApplyChangeFailedEventArgs e)
{
   MessageBox.Show(
     string.Format(
             "{1}{0}{2}.{0}{0}" +
             "{3}   {4}.{0}{0}" +
             "{5}{0}{6}.{0}",
             Environment.NewLine,
             "Conflict Type:",
             e.Conflict.ConflictType.ToString(),
             "CustomerID:",
             e.Conflict.ClientChange.Rows[0]
                        ["CustomerID"].ToString(),
             "CompanyName:",
             e.Conflict.ClientChange.Rows[0]
                        ["CompanyName"].ToString()));
}

Figure 17 shows the result from a scenario in which a row was inserted at the server concurrently with a row of the same primary key being inserted at the device.

Figure 17. Displaying the Failed Row


This concludes our brief demonstration of developing a Data Synchronization Service. Figures 18 and 19 diagram our resulting Smart Device application and Data Synchronization Service, respectively.

Figure 18. The Smart Device Application


Figure 19. The Data Synchronization Service


2.5. Some Closing Notes on Data Synchronization Services

As the need arises to add functionality to your solution, such as automatic conflict resolution or business logic, you can do so in any of several ways, three of which are mentioned here.

  • Add logic to the client program, such as we did with our simple conflict detection logic. This is a good solution for logic that is applicable to this client only.

  • Add logic to the service program. This is a slightly unstable solution, as it may need to be redone if future modifications are generated by rerunning the Configure Data Synchronization Wizard.

  • Derive classes that contain your functionality from the Microsoft.Synchronization classes and use these classes in your service. This is a good solution for logic that must be applied to the data regardless of the application that is manipulating that data.

In summary, Data Synchronization Services do the following:

  • Provide a complete solution for the synchronization of data that is gathered by Occasionally Connected Applications

  • Allow the Smart Device application to handle synchronization failures

  • Encapsulate the synchronization infrastructure functionality within a SyncAgent object and its contained objects

  • Provide more capability than RDA (albeit by being more complex)

  • Differ from Merge Replication in that they are controlled by the developer rather than the database administrator, and this allows for more application of business logic

Other  
  •  # BlackBerry Java Application Development : Networking - HTTP Basics
  •  Mobile Phone Game Programming : Analyzing 2D Sprite Animation
  •  Mobile Phone Game Programming : Understanding Animation
  •  Synchronizing Mobile Data - Using Merge Replication (part 2) - Programming for Merge Replication
  •  Synchronizing Mobile Data - Using Merge Replication (part 1) - Using Good Design to Avoid Synchronization Failures
  •  Windows Phone 7 Advanced Programming Model : Advanced Data Binding (part 4) - Data Bind to Anything
  •  Windows Phone 7 Advanced Programming Model : Advanced Data Binding (part 3) - Showing Progress & Lazy Load Images
  •  Windows Phone 7 Advanced Programming Model : Advanced Data Binding (part 2) - Syndicated Services
  •  Windows Phone 7 Advanced Programming Model : Advanced Data Binding (part 1)
  •  Beginning Android 3 : The Input Method Framework - Fitting In
  •  Mobile Application Security : Mobile Geolocation - Geolocation Methods & Geolocation Implementation
  •  Mobile Application Security : SMS Security - Application Attacks & Walkthroughs
  •  iPad SDK : Popovers - The Stroke Width Popover
  •  iPad SDK : Popovers - The Font Size Popover
  •  Beginning Android 3 : The Input Method Framework - Tailored to Your Needs
  •  Beginning Android 3 : Working with Containers - Scrollwork
  •  Mobile Application Security : SMS Security - Protocol Attacks (part 2)
  •  Mobile Application Security : SMS Security - Protocol Attacks (part 1)
  •  Mobile Application Security : SMS Security - Overview of Short Message Service
  •  iPad SDK : Popovers - The Font Name Popover (part 2)
  •  
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Bamboo Splash - Powerful Specs And Friendly Interface
    Powered By Windows (Part 2) - Toshiba Satellite U840 Series, Philips E248C3 MODA Lightframe Monitor & HP Envy Spectre 14
    MSI X79A-GD65 8D - Power without the Cost
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Personalize Your iPhone Case
    Speed ​​up browsing with a faster DNS
    Using and Configuring Public Folder Sharing
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 1)
    Google, privacy & you (Part 1)
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Microsoft Surface With Windows RT - Truly A Unique Tablet
    Network Configuration & Troubleshooting (Part 1)
    Panasonic Lumix GH3 – The Fastest Touchscreen-Camera (Part 2)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Exchange Server 2010 : Track Exchange Performance (part 2) - Test the Performance Limitations in a Lab
    Extra Network Hardware Round-Up (Part 2) - NAS Drives, Media Center Extenders & Games Consoles
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Google’s Data Liberation Front (Part 2)
    Datacolor SpyderLensCal (Part 1)