Simple Example

The following example does a simple replication from a handheld database to the desktop database:

ReplicationExample.java: replicate
01public static void replicate(){ 02 ObjectContainer desktop=Db4o.openFile(DTFILENAME); 03 ObjectContainer handheld=Db4o.openFile(HHFILENAME); 04 // Setup a replication session 05 ReplicationSession replication = Replication.begin(handheld, desktop); 06 07 /* 08 * There is no need to replicate all the objects each time. 09 * objectsChangedSinceLastReplication methods gives us 10 * a list of modified objects 11 */ 12 ObjectSet changed = replication.providerA().objectsChangedSinceLastReplication(); 13 14 while (changed.hasNext()) { 15 replication.replicate(changed.next()); 16 } 17 18 replication.commit(); 19 }
We start by opening two ObjectContainers. The next line creates the ReplicationSession. This object contains all of the replication-related logic.

After creating the session, there is an interesting line:

Java:

ObjectSet changed = replication.providerA().objectsChangedSinceLastReplication();

This line of code will get the provider associated with the first of our sources (the handheld ObjectContainer in this case). Then it finds all of the objects that have been updated or created. The new/modified objects will be returned in an enumerable ObjectSet.

After that comes a simple loop where the resulting objects are replicated one at a time.
The replication.commit() call at the end is important. This line will save all of the changes we have made, and end any needed transactions. Forgetting to make this call will result in your replication changes being discarded when your application ends, or your ObjectContainers are closed.
The #commit() calls also mark all objects as replicated. Therefore, changed/new objects that are not replicated in this session will be marked as replicated.