A db4o-to-db4o replication is easy to use and easy to set up. There are only two steps required to get the replication started.
First ensure that you have added the dRS-jar to your project. See "Getting Started"
The replication process needs two things. An UUID for each object to identify it across multiple databases. Furthermore every object needs a version-number to detect updates. By default db4o doesn't generate a UUID nor keeps timestamps. So you need to configure this explicitly:
configuration.file().generateUUIDs(ConfigScope.GLOBALLY);
configuration.file().generateCommitTimestamps(true);
After this preparation you can store, retrieve and update normally. To start the replication-process open the two databases which take part in the replication. Then create an replication-session by passing the replication partners to the replication factory.
ObjectContainer desktopDatabase = openDatabase(DESKTOP_DATABASE_NAME); ObjectContainer mobileDatabase = openDatabase(MOBILE_DATABASE_NAME); Db4oEmbeddedReplicationProvider providerA = new Db4oEmbeddedReplicationProvider(desktopDatabase); Db4oEmbeddedReplicationProvider providerB = new Db4oEmbeddedReplicationProvider(mobileDatabase); ReplicationSession replicationSession = Replication.begin(providerA, providerB); // set the replication-direction from the desktop database to the mobile database. replicationSession.setDirection(replicationSession.providerA(), replicationSession.providerB());
Now the system is ready for replication. First request all changes from a replication partner. The replication session will return all objects which have been created or updated since the last replication. Then iterate over the returned objects and replicate object by object.
ObjectSet changes = replicationSession.providerA().objectsChangedSinceLastReplication();
for (Object changedObject : changes) {
replicationSession.replicate(changedObject);
}
replicationSession.commit();
To complete the replication, call commit on the replication session. This commit stores all changes in replication partners and finishes the running transactions. Furthermore it marks the objects as replicated to ensure that future replications only include the new changes. Without the commit call the replication-changes will be discarded!