<?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="db4o Databases Replication" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Bidirectional Replication</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/db4o_databases_replication/bi-directional_replication.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../db4o_databases_replication.htm">db4o Databases Replication</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Bidirectional Replication</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="kanchor4"></a>Bidirectional Replication</h1> <p>It's quite often the case that you need replication in both directions. For example when you have a mobile device which keeps the data in sync with a desktop PC. Changes on the both partners have to be replicated to the other partner.</p> <p>First, make sure that you've prepared and configured db4o properly. <a href="simple_example.htm" target="" title="" alt="" class="MCXref" xrefformat="See "{paratext}"">See "Simple Example"</a></p> <p>Start a replication-session with the two replication partners. Note that no replication direction is set.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName); IObjectContainer mobileDatabase = OpenDatabase(MobileDatabaseName); IReplicationProvider dektopReplicationProvider = new Db4oEmbeddedReplicationProvider(desktopDatabase); IReplicationProvider mobileReplicationProvider = new Db4oEmbeddedReplicationProvider(mobileDatabase); IReplicationSession replicationSession = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider);</pre> <div class="codesnippet-meta">Db4oReplicationExamples.cs: Prepare bidirectional replication <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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 desktopDatabase As IObjectContainer = OpenDatabase(DesktopDatabaseName) Dim mobileDatabase As IObjectContainer = OpenDatabase(MobileDatabaseName) Dim dektopReplicationProvider As IReplicationProvider _ = New Db4oEmbeddedReplicationProvider(desktopDatabase) Dim mobileReplicationProvider As IReplicationProvider _ = New Db4oEmbeddedReplicationProvider(mobileDatabase) Dim replicationSession As IReplicationSession _ = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider)</pre> <div class="codesnippet-meta">Db4oReplicationExamples.vb: Prepare bidirectional replication <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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>Then get the changed objects from both replication partners. Each replication partner returns the updated and created object in a object-set. Then iterate over both object-sets and replicate the changes. </p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">// First get the changes of the two replication-partners IObjectSet changesOnDesktop = replicationSession.ProviderA().ObjectsChangedSinceLastReplication(); IObjectSet changesOnMobile = replicationSession.ProviderB().ObjectsChangedSinceLastReplication(); // then iterate over both change-sets and replicate it foreach (object changedObjectOnDesktop in changesOnDesktop) { replicationSession.Replicate(changedObjectOnDesktop); } foreach (object changedObjectOnMobile in changesOnMobile) { replicationSession.Replicate(changedObjectOnMobile); } replicationSession.Commit();</pre> <div class="codesnippet-meta">Db4oReplicationExamples.cs: Bidirectional replication <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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">' First get the changes of the two replication-partners Dim changesOnDesktop As IObjectSet _ = replicationSession.ProviderA().ObjectsChangedSinceLastReplication() Dim changesOnMobile As IObjectSet _ = replicationSession.ProviderB().ObjectsChangedSinceLastReplication() ' then iterate over both change-sets and replicate it For Each changedObjectOnDesktop As Object In changesOnDesktop replicationSession.Replicate(changedObjectOnDesktop) Next For Each changedObjectOnMobile As Object In changesOnMobile replicationSession.Replicate(changedObjectOnMobile) Next replicationSession.Commit()</pre> <div class="codesnippet-meta">Db4oReplicationExamples.vb: Bidirectional replication <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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>