You are here: Advanced Replication Strategies > Dealing with Conflicts

Dealing with Conflicts

When replicated objects are changed independent and the replicated again, conflict are inevitable. The default behavior of dRS is to throw a ReplicationConflictException in such a situation. This prevents corruption, but doesn't help to resolve the conflict. Fortunately dRS allows you to handle such a conflict situation properly.

To deal with conflicts you use a event listener. See "Events" . The event has also information about conflicts. When a change conflicts with another change, the conflict flag is set on the replication event. Then you can decide which state is actually replicated. For example: When a conflict occurs you could always take the state of one replication partner.

ReplicationSession replicationSession = Replication.begin(providerA, providerB,
        new ReplicationEventListener() {
            public  void onReplicate(ReplicationEvent replicationEvent) {
                if (replicationEvent.isConflict()) {
                    ObjectState stateOfTheDesktop = replicationEvent.stateInProviderA();
                    replicationEvent.overrideWith(stateOfTheDesktop);
                }
            }
        });
AdvancedReplicationExamples.java: Deal with conflicts

Of course there are more advanced strategies possible. For example can compare the timestamp's of the conflicting changes and take the newer change. Note that the clocks of disconnected devices are not perfectly synchronized.

ReplicationSession replicationSession = Replication.begin(providerA, providerB,
        new ReplicationEventListener() {
            public  void onReplicate(ReplicationEvent replicationEvent) {
                if (replicationEvent.isConflict()) {
                    ObjectState stateDesktop = replicationEvent.stateInProviderA();
                    ObjectState stateMobile = replicationEvent.stateInProviderB();

                    if (stateDesktop.modificationDate() >= stateMobile.modificationDate()) {
                        replicationEvent.overrideWith(stateDesktop);
                    } else {
                        replicationEvent.overrideWith(stateMobile);
                    }
                }
            }
        });
AdvancedReplicationExamples.java: Take latest change