Gremlin transactions in Neptune - Amazon Neptune
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Gremlin transactions in Neptune

There are several contexts within which Gremlin transactions are executed. When working with Gremlin it is important to understand the context you are working within and what its implications are:

  • Script-based   –   Requests are made using text-based Gremlin strings, like this:

    • Using the Java driver and Client.submit(string).

    • Using the Gremlin console and :remote connect.

    • Using the HTTP API.

  • Bytecode-based   –   Requests are made using serialized Gremlin bytecode typical of Gremlin Language Variants(GLV).

    For example, using the Java driver, g = traversal().withRemote(...).

For either of the above contexts, there is the additional context of the request being sent as sessionless or as bound to a session.

Sessionless requests

When sessionless, a request is equivalent to a single transaction.

For scripts, the implication is that one or more Gremlin statements sent in a single request will commit or rollback as a single transaction. For example:

Cluster cluster = Cluster.open(); Client client = cluster.connect(); // sessionless // 3 vertex additions in one request/transaction: client.submit("g.addV();g.addV();g.addV()").all().get();

For bytecode, a sessionless request is made for each traversal spawned and executed from g:

GraphTraversalSource g = traversal().withRemote(...); // 3 vertex additions in three individual requests/transactions: g.addV().iterate(); g.addV().iterate(); g.addV().iterate(); // 3 vertex additions in one single request/transaction: g.addV().addV().addV().iterate();

Requests bound to a session

When bound to a session, multiple requests can be applied within the context of a single transaction.

For scripts, the implication is that there is no need to concatenate together all of the graph operations into a single embedded string value:

Cluster cluster = Cluster.open(); Client client = cluster.connect(sessionName); // session // 3 vertex additions in one request/transaction: client.submit("g.addV();g.addV();g.addV()").all().get(); client.close(); // 3 vertex additions in three requests, but one transaction: client.submit("g.addV()").all().get(); client.submit("g.addV()").all().get(); client.submit("g.addV()").all().get(); client.close();

For bytecode, after TinkerPop 3.5.x, the transaction can be explicitly controlled and the session managed transparently. Gremlin Language Variants (GLV) support Gremlin's tx() syntax to commit() or rollback() a transaction as follows:

GraphTraversalSource g = traversal().withRemote(conn); Transaction tx = g.tx(); // Spawn a GraphTraversalSource from the Transaction. // Traversals spawned from gtx are executed within a single transaction. GraphTraversalSource gtx = tx.begin(); try { gtx.addV('person').iterate(); gtx.addV('software').iterate(); tx.commit(); } catch (Exception ex) { tx.rollback(); }

Although the example above is written in Java, you can also use this tx() syntax in Python, Javascript and .NET.