Bi-Directional Replication


The previous example copied all new or modified objects from the handheld device to the desktop database. What if we want to go the other way? Well, we only have to add one more loop:

ReplicationExample.java: replicateBiDirectional
01public static void replicateBiDirectional(){ 02 ObjectContainer desktop=Db4o.openFile(DTFILENAME); 03 ObjectContainer handheld=Db4o.openFile(HHFILENAME); 04 ReplicationSession replication = Replication.begin(handheld, desktop); 05 ObjectSet changed = replication.providerA().objectsChangedSinceLastReplication(); 06 while (changed.hasNext()) { 07 replication.replicate(changed.next()); 08 } 09 // Add one more loop for bi-directional replication 10 changed = replication.providerB().objectsChangedSinceLastReplication(); 11 while(changed.hasNext()) { 12 replication.replicate(changed.next()); 13 } 14 15 replication.commit(); 16 }
Now our handheld contains all of the new and modified records from our desktop.

Easy, isn't it? Now, if there had been any modifications made to the destination database, the two are now both in sync with each other.