After setting up JDO and the event processing we're ready to start the replication.
Note: Replication with VOD requires at least VOD version 8.0.1.3 with JVI installed.
For the VOD replication following libraries need to be in the class-path:
From the VOD distribution
From the db4o / dRS distribution
The first thing you need to do is preparing db4o for replication. Enable the UUID and version-number support for your database. UUID's are used to recognize objects across the database boundary. The version number is used the track changes.
Because classes are enhanced for JDO they have additional fields. To avoid that these artificial fields are stored in the db4o database you need to use the JDOReflector. This reflector recognized JDO -aware objects and treats them differently.
EmbeddedConfiguration config = Db4oEmbedded.newConfiguration(); config.file().generateUUIDs(ConfigScope.GLOBALLY); config.file().generateCommitTimestamps(true); config.common().reflectWith( new JdoReflector(Thread.currentThread().getContextClassLoader()));
After this preparation you can store, retrieve and update normally. To start the replication-process you need to create the two replication partners. Open a replication partner for db4o like this:
ReplicationProvider mobileDatabase
= new Db4oEmbeddedReplicationProvider(container);
Then open a replication provider for VOD. First create a new VodDatabase instance by passing the JDO PersistenceManagerFactory or the configuration properties to the constructor.
Second configure the host and port of the event processor with the configureEventProcessor method. Use the port which you've configured when setting up the event processor. It's the port you've configured with the '-eventProcessorPort'-configuration.
Then create the VOD replication provider. For the VOD replication provider you should additionally add all classes which you want to replicate. Why is this necessary? In VOD you usually have a huge database and you only want to replicate parts of it to db4o. Therefore you can specify which classes are replicated. In case you don’t specify anything only the types which have been already replicate once are replicated.
VodDatabase vodDatabase = new VodDatabase(factory); vodDatabase.configureEventProcessor("localhost",4088); VodReplicationProvider centralDatabase = new VodReplicationProvider(vodDatabase); centralDatabase.listenForReplicationEvents(Car.class, Pilot.class);
After that you can start the replication. Request the changes from the db4o replication provider. Then iterate over the changed objects and replicate them. Commit to complete the replication process.
ReplicationSession replicationSession =
Replication.begin(mobileDatabase, centralDatabase);
ObjectSet changesOnMobileDB = mobileDatabase.objectsChangedSinceLastReplication();
for (Object changedObject : changesOnMobileDB) {
replicationSession.replicate(changedObject);
}
replicationSession.commit();
After this we've replicated the objects from db4o to VOD.
To replicate from VOD to db4o ensure that you have configured the VOD replication provider properly. You need to add the types which you're going to replicate.
VodDatabase vodDatabase = new VodDatabase(factory); vodDatabase.configureEventProcessor("localhost",4088); VodReplicationProvider centralDatabase = new VodReplicationProvider(vodDatabase); centralDatabase.listenForReplicationEvents(Car.class, Pilot.class);
To replicate from VOD to db4o you just need to change the replication direction. Take the changes from the VOD database and replicate them to db4o.
ReplicationSession replicationSession =
Replication.begin(mobileDatabase, centralDatabase);
ObjectSet changesOnVOD = centralDatabase.objectsChangedSinceLastReplication();
for (Object changedObject : changesOnVOD) {
replicationSession.replicate(changedObject);
}
replicationSession.commit();
To replicate bidirectional you need to get the changes from both databases and then replicate those changes.
ReplicationSession replicationSession = Replication.begin(mobileDatabase, centralDatabase); ObjectSet changesOnDB4O = mobileDatabase.objectsChangedSinceLastReplication(); for (Object changedObject : changesOnDB4O) { replicationSession.replicate(changedObject); } ObjectSet changesOnVOD = centralDatabase.objectsChangedSinceLastReplication(); for (Object changedObject : changesOnVOD) { replicationSession.replicate(changedObject); } replicationSession.commit();