orientechnologies / orientdb-gremlin

TinkerPop3 Graph Structure Implementation for OrientDB
Apache License 2.0
92 stars 32 forks source link

Temporary ID in different transactions #154

Open To-om opened 6 years ago

To-om commented 6 years ago

When a create a vertex, I get a temporary ID (ends with ":-2"). I can retrieve that vertex with the string representation of the ID only in the same transaction. If I open a new transaction, the ID is unusable

OrientGraph graph1 = factory.getTx();
Vertex v = graph1.addVertex(labelVertex);
String vid = v.id().toString();
Assert.assertEquals(1, graph1.V(vid).toList().size()); // succeeds
graph1.tx().commit();

OrientGraph graph2 = factory.getTx();
Assert.assertEquals(1, graph2.V(vid).toList().size()); // fails

String newId = vid.substring(0, vid.length() - 2) + "0";
Assert.assertEquals(1, graph2.V(vid).toList().size()); // succeeds
graph2.tx().commit();

I suppose replacing ":-2" by ":0" is not the solution. How can I provide a stable ID to client (I am in a web application context) ?

wolf4ood commented 6 years ago

Hi @To-om

OrientDB uses temporary rids in transactions and are resolved at commit time. That means that if you store a termporary rids in a String you will get the temporary rid.

if you change the code in this way it works

OrientGraphFactory factory = new OrientGraphFactory("plocal:/tmp/test");

    OrientGraph graph1 = factory.getTx();
    String labelVertex = "Test";
    Vertex v = graph1.addVertex(labelVertex);
    Assert.assertEquals(1, graph1.traversal().V(v.id()).toList().size()); // succeeds
    graph1.tx().commit();
    String vid = v.id().toString();

    OrientGraph graph2 = factory.getTx();
    Assert.assertEquals(1, graph2.traversal().V(vid).toList().size()); // fails

    String newId = vid.substring(0, vid.length() - 2) + "0";
    Assert.assertEquals(1, graph1.traversal().V(vid).toList().size()); // succeeds
    graph2.tx().commit();
To-om commented 6 years ago

Thank you for your answer. In my case, transaction encompasses the whole HTTP request process, including the build of the response. I think that it is not a good idea to commit the transaction before each id obtained because the process could fail after that, rollback won't be possible.

wolf4ood commented 6 years ago

@To-om

the idea is to keep all the newly created vertices edges and then after commit use the api

v.id() 

to get the persisted record id