The following example does a simple replication from a handheld database to the desktop database:
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
}
After creating the session, there is an interesting line:
Java:
ObjectSet changed =
replication.providerA().objectsChangedSinceLastReplication();
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.