2
votes

I have an orientdb setup with a class called MessageLog with a property messageId that has a constraint MANADATORY and NOT NULL. If i try to insert a record from the console with a null messageId it raises an exception telling that the property cannot be null. But when i do a simple insertion from the Java API the record is inserted with the property value null. How is that possible.

The java code is:

import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;

public class OrientDbTrials {

public static void main(String[] args) {

    OrientGraph graph = new OrientGraph("remote:localhost/blah","root","*****");
    System.out.println("Connected to the db.");

    Vertex messageLog = graph.addVertex("class:MessageLog");

    System.out.println("Created new vertex : " + messageLog.toString());
    messageLog.setProperty("messageId", null);
    graph.commit();
    System.out.println("Successfully saved it.");

    }

}

Can somebody please explain this.

1

1 Answers

2
votes

From http://www.orientechnologies.com/docs/1.7.8/orientdb.wiki/Graph-Database-Tinkerpop.html

Every time the graph is modified an implicit transaction is started automatically if no previous transaction was running. Transactions are committed automatically when the graph is closed by calling the shutdown() method or by explicit commit(). To rollback changes call the rollback() method. Changes inside a transaction will be temporary till the commit or the close of the graph instance. Concurrent threads or external clients can see the changes only when the transaction has been fully committed.

Because you are not defining how to handle exceptions, I think that when one is raised the graph is being closed and thus committing the change. When I run the code you give in a try catch block, I get an exception.

OrientGraph graph = new OrientGraph("remote:localhost/blah","root","*****");
System.out.println("Connected to the db.");
try { Vertex messageLog = graph.addVertex("class:MessageLog");

      System.out.println("Created new vertex : " + messageLog.toString());
      messageLog.setProperty("messageId", null);
      graph.commit();
} catch(Exception e) {
  graph.rollback();
}
finally {
  graph.shutdown();
}

That code (pardon the ugly) raises this exception

java.lang.IllegalArgumentException: Property value can not be null

And because I handle the exception with a graph.rollback() the vertex doesn't show up in the graph.

What's interesting is that just having the mandatory property doesn't seem to be enough, i.e if you never fill the field with a null in the first place it will allow the commit, not raising an exception. I haven't looked into it, but maybe it has something to do with a graph instance being mixed-schema by default or something like that? I hope someone else knows.