Search code examples
javaorientdb

OrientDB. Setting ConflictStrategy throwing an exception


I have OObjectDatabaseTx. I want to set my own conflict strategy using api.

OObjectDatabaseTx db;
db.setConflictStrategy(new ORecordConflictStrategy() {
            @Override
            public byte[] onUpdate(OStorage storage, byte iRecordType, ORecordId rid, ORecordVersion iRecordVersion, byte[] iRecordContent, ORecordVersion iDatabaseVersion) {
                <implementation>;
            }

            @Override
            public String getName() {
                return "SOME NAME";
            }
        });

On execution i'm getting an exception sarying that operation is not supported.

10:40:48,718 INFO  [com.ats.vis.services.transaction.TransactionManager] (TransactionExecutor[UML]) [__MAIN_WS__] ERROR:: java.lang.UnsupportedOperationException: setConflictStrategy
at com.orientechnologies.orient.client.remote.OStorageRemoteThread.setConflictStrategy(OStorageRemoteThread.java:318) [orientdb-client-2.1.19.jar:2.1.19]
at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.setConflictStrategy(ODatabaseDocumentTx.java:1122) [orientdb-core-2.1.19.jar:2.1.19]
at com.orientechnologies.orient.object.db.OObjectDatabaseTx.setConflictStrategy(OObjectDatabaseTx.java:760) [orientdb-object-2.1.19.jar:2.1.19]

Is there any way to set conflict strategy?


Solution

  • You're setting it in the client, but this is not possible, because it's the server that use it. You should rather install it in the server if you're using OrientDB with "remote" protocol. How to do that?

    It's very easy, write a Server Plugin that in the startup() register it self as database listener. In this way, once you implement the onOpen() and onCreate() methods, you can install your conflict strategy as you've already done before. Example (not tried):

    public class MyPlugin OServerPluginAbstract implements ODatabaseLifecycleListener {
      @Override
      public void startup() {
        Orient.instance().addDbLifecycleListener(this);
      }
    
      @Override
      public void onOpen(final ODatabaseInternal db) {
        db.setConflictStrategy(new ORecordConflictStrategy() {
            @Override
            public byte[] onUpdate(OStorage storage, byte iRecordType, ORecordId rid, ORecordVersion iRecordVersion, byte[] iRecordContent, ORecordVersion iDatabaseVersion) {
                <implementation>;
            }
    
            @Override
            public String getName() {
                return "SOME NAME";
            }
        });
      }
    
      @Override
      public void onOpen(final ODatabaseInternal db) {
        onOpen(db);
      }
      // BOILERPLATE CODE MISSING
    }
    

    And then register it in config/orientdb-server-config.xml file as handler:

        <handler class="com.orientechnologies.orient.server.handler.OJMXPlugin">
            <parameters>
            </parameters>
        </handler>