<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Advanced Replication Strategies" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Dealing with Conflicts</title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../../Resources/Code/prettify.js"> </script> <script src="../../Resources/Code/lang-vb.js"> </script> <script src="../../Resources/Code/jquery.min.js"> </script> <script src="../../Resources/Code/ZeroClipboard.js"> </script> <script src="../../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../index_CSH.html#db4o_replication_system_drs/advanced_replication_strategies/dealing_with_conflicts.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../advanced_replication_strategies.htm">Advanced Replication Strategies</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Dealing with Conflicts</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1><a name="kanchor8"></a>Dealing with Conflicts</h1> <p>When replicated objects are changed independent and the replicated again, conflict are inevitable. The default behavior of dRS is to throw a ReplicationConflictException in such a situation. This prevents corruption, but doesn't help to resolve the conflict. Fortunately dRS allows you to handle such a conflict situation properly.</p> <p>To deal with conflicts you use a event listener. <a href="events.htm" target="" title="" alt="" class="MCXref" xrefformat="See "{paratext}"">See "Events"</a> . The event has also information about conflicts. When a change conflicts with another change, the conflict flag is set on the replication event. Then you can decide which state is actually replicated. For example: When a conflict occurs you could always take the state of one replication partner.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">class SimpleConflictResolvingListener : IReplicationEventListener { public void OnReplicate(IReplicationEvent replicationEvent) { if (replicationEvent.IsConflict()) { IObjectState stateOfTheDesktop = replicationEvent.StateInProviderA(); replicationEvent.OverrideWith(stateOfTheDesktop); } } }</pre> <div class="codesnippet-meta">AdvancedReplicationExamples.cs: Conflict resolving listener <div class="codedownload"><a href="../../CodeExamples/drs/advanced/Example-drs-advanced-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Class SimpleConflictResolvingListener Implements IReplicationEventListener Public Sub OnReplicate(ByVal replicationEvent As IReplicationEvent) _ Implements IReplicationEventListener.OnReplicate If replicationEvent.IsConflict() Then Dim stateOfTheDesktop As IObjectState = replicationEvent.StateInProviderA() replicationEvent.OverrideWith(stateOfTheDesktop) End If End Sub End Class</pre> <div class="codesnippet-meta">AdvancedReplicationExamples.vb: Conflict resolving listener <div class="codedownload"><a href="../../CodeExamples/drs/advanced/Example-drs-advanced-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IReplicationSession replicationSession = Replication.Begin(desktopRelicationPartner, mobileRelicationPartner, new SimpleConflictResolvingListener());</pre> <div class="codesnippet-meta">AdvancedReplicationExamples.cs: Deal with conflicts <div class="codedownload"><a href="../../CodeExamples/drs/advanced/Example-drs-advanced-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim replicationListener As IReplicationEventListener = New SimpleConflictResolvingListener() Dim replicationSession As IReplicationSession _ = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider, replicationListener)</pre> <div class="codesnippet-meta">AdvancedReplicationExamples.vb: Deal with conflicts <div class="codedownload"><a href="../../CodeExamples/drs/advanced/Example-drs-advanced-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p>Of course there are more advanced strategies possible. For example can compare the timestamp's of the conflicting changes and take the newer change. Note that the clocks of disconnected devices are not perfectly synchronized.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">class TakeLatestModificationOnConflictListener : IReplicationEventListener { public void OnReplicate(IReplicationEvent replicationEvent) { if (replicationEvent.IsConflict()) { IObjectState stateOfTheDesktop = replicationEvent.StateInProviderA(); IObjectState stateOfTheMobile = replicationEvent.StateInProviderB(); if (stateOfTheDesktop.ModificationDate() >= stateOfTheMobile.ModificationDate()) { replicationEvent.OverrideWith(stateOfTheDesktop); } else { replicationEvent.OverrideWith(stateOfTheMobile); } } } }</pre> <div class="codesnippet-meta">AdvancedReplicationExamples.cs: Listener which takes latest changes <div class="codedownload"><a href="../../CodeExamples/drs/advanced/Example-drs-advanced-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Class TakeLatestModificationOnConflictListener Implements IReplicationEventListener Public Sub OnReplicate(ByVal replicationEvent As IReplicationEvent) _ Implements IReplicationEventListener.OnReplicate If replicationEvent.IsConflict() Then Dim stateOfTheDesktop As IObjectState = replicationEvent.StateInProviderA() Dim stateOfTheMobile As IObjectState = replicationEvent.StateInProviderB() If stateOfTheDesktop.ModificationDate() >= stateOfTheMobile.ModificationDate() Then replicationEvent.OverrideWith(stateOfTheDesktop) Else replicationEvent.OverrideWith(stateOfTheMobile) End If End If End Sub End Class</pre> <div class="codesnippet-meta">AdvancedReplicationExamples.vb: Listener which takes latest changes <div class="codedownload"><a href="../../CodeExamples/drs/advanced/Example-drs-advanced-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IReplicationSession replicationSession = Replication.Begin(desktopRelicationPartner, mobileRelicationPartner, new TakeLatestModificationOnConflictListener());</pre> <div class="codesnippet-meta">AdvancedReplicationExamples.cs: Take latest change <div class="codedownload"><a href="../../CodeExamples/drs/advanced/Example-drs-advanced-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim replicationListener As IReplicationEventListener = _ New TakeLatestModificationOnConflictListener() Dim replicationSession As IReplicationSession _ = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider, replicationListener)</pre> <div class="codesnippet-meta">AdvancedReplicationExamples.vb: Take latest change <div class="codedownload"><a href="../../CodeExamples/drs/advanced/Example-drs-advanced-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>