Search code examples
gremlintinkerpop3datastax-enterprise-graph

Transactions in Tinkerpop3 DSE


This is a question to understand the transaction semantics in DSE graph via TinkerPop3 Java API

As I understand, when you have DseSession.executeGraph(String query) is executed in a single transaction.

But what is the mechanism to achieve the same using TinkerPop3 Java API ? Say I want to add a vertex and then drop a few edges and then add a new vertex etc.,

I tried

g.V("{~label=company, id=12345}").drop().addV("company").property("id", "1").next()

This didn't work as I intended. (Dropping the company and adding one in a single transaction). The first vertex did indeed get deleted, however the new company was never created.

I got below exception

Exception in thread "main" java.util.NoSuchElementException
    at com.datastax.dse.graph.internal.DseRemoteTraversal.nextTraverser(DseRemoteTraversal.java:60)

Solution

  • The drop() step "kills" the traverser you are deleting, meaning nothing is returned from drop(). You can see that if you try this:

    gremlin> g.V().has('name','marko').drop().next()
    java.util.NoSuchElementException
    Type ':help' or ':h' for help.
    Display stack trace? [yN]
    

    Without a traverser there is nothing to feed forward to your addV() step that follows. There might be a nicer way to do this, but you can work around that problem by using inject() to add a throwaway value as a traverser that will feed into your addV(). In this case, you deleted one thing, so you need to put one traverser back to trigger the single call to addV() thus:

    gremlin> g.V().has('name','marko').drop().
    ......1>   inject(1).
    ......2>   addV('person').property('name','stephen').next()
    ==>v[13]
    

    I also wonder if this is a nice way to do it:

    gremlin> g.V().has('name','marko').
    ......1>   union(drop(),
    ......2>         addV('person').property('name','stephen'))
    ==>v[13]