You are here: db4o Databases Replication > Bidirectional Replication

Bidirectional Replication

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.

First, make sure that you've prepared and configured db4o properly. See "Simple Example"

Start a replication-session with the two replication partners. Note that no replication direction is set.

IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);
IObjectContainer mobileDatabase = OpenDatabase(MobileDatabaseName);

IReplicationProvider dektopReplicationProvider
    = new Db4oEmbeddedReplicationProvider(desktopDatabase);
IReplicationProvider mobileReplicationProvider
    = new Db4oEmbeddedReplicationProvider(mobileDatabase);

IReplicationSession replicationSession
    = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider);
Db4oReplicationExamples.cs: Prepare bidirectional replication
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)
Db4oReplicationExamples.vb: Prepare bidirectional replication

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.

// 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();
Db4oReplicationExamples.cs: Bidirectional replication
' 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()
Db4oReplicationExamples.vb: Bidirectional replication