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-assembly 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;
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.
IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName); IObjectContainer mobileDatabase = OpenDatabase(MobileDatabaseName); IReplicationProvider dektopReplicationProvider = new Db4oEmbeddedReplicationProvider(desktopDatabase); IReplicationProvider mobileReplicationProvider = new Db4oEmbeddedReplicationProvider(mobileDatabase); IReplicationSession replicationSession = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider); // set the replication-direction from the desktop database to the mobile database. replicationSession.SetDirection(replicationSession.ProviderA(), replicationSession.ProviderB());
Dim desktopDatabase As IObjectContainer = OpenDatabase(DesktopDatabaseName) Dim mobileDatabase As IObjectContainer = OpenDatabase(MobileDatabaseName) Dim dektopReplicationProvider As IReplicationProvider _ = New Db4oEmbeddedReplicationProvider(desktopDatabase) Dim mobileReplicationProvider As IReplicationProvider _ = New Db4oEmbeddedReplicationProvider(mobileDatabase) Dim replicationSession As IReplicationSession _ = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider) ' 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.
IObjectSet changes = replicationSession.ProviderA().ObjectsChangedSinceLastReplication(); foreach (object changedObject in changes) { replicationSession.Replicate(changedObject); } replicationSession.Commit();
Dim changes As IObjectSet = replicationSession.ProviderA().ObjectsChangedSinceLastReplication() For Each changedObject As Object In changes replicationSession.Replicate(changedObject) Next 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!