You are here: Replication With RDBMS > Bidirectional Replication

Bidirectional Replication

In order to replicate objects from Hibernate to db4o you need to do some additional work. dRS needs to know which objects have changed since the last replication. For this some meta data is maintained.You need to register a dRS event handler on every Hibernate-session you're using. This listener will maintain the meta-data. If an object is stored on a session without this listener, the update is not replicated. That also means dRS replication only works when you persist all object through Hibernate

SessionFactory sessionFactory = hibernateConfig.buildSessionFactory();
Session session = sessionFactory.openSession();
ReplicationConfigurator.install(session, hibernateConfig);
Db4oRDBMSReplicationExamples.java: Install the listeners to the session

After that dRS is ready to replicate in both directions. Again, set up the replication-session. After that you can replicate the changes of the two providers in both directions.

Configuration hibernateConfig
        = new Configuration().configure("com/db4odoc/drs/rdms/hibernate.cfg.xml");
ObjectContainer container = openDB();

Db4oEmbeddedReplicationProvider providerA = new Db4oEmbeddedReplicationProvider(container);
HibernateReplicationProvider providerB = new HibernateReplicationProvider(hibernateConfig);

ReplicationSession replicationSession = Replication.begin(providerA, providerB);

final ObjectSet changesInDb4o = replicationSession.providerA()
        .objectsChangedSinceLastReplication();
for (Object changedObject : changesInDb4o) {
    replicationSession.replicate(changedObject);
}
final ObjectSet changesInHibernate = replicationSession.providerB()
        .objectsChangedSinceLastReplication();
for (Object changedObject : changesInHibernate) {
    replicationSession.replicate(changedObject);
}
Db4oRDBMSReplicationExamples.java: Bidirectional replication