rey mysterio jr III
rey mysterio jr III

Reputation: 13

ArangoDB Use Graph Module within Transactions

I'm using the Java driver (4.1) and I need the guarantees of the named graph operations, but I need to do several of these higher level operations in a transaction.

Say for instance I want to add several vertices to a collection in one go, and I want to rollback all operations upon any failure. If I'm using the Java driver, what I'd like is to do something like this:

db.beginTransaction() ; // this method doesn't exist
db.graph("myGraph").vertexCollection(vertexCollectionName1).insertVertex(a);
db.graph("myGraph").vertexCollection(vertexCollectionName1).insertVertex(b);
db.graph("myGraph").vertexCollection(vertexCollectionName2).insertVertex(c);
// ...so on...
db.executeTransaction(); // rollback all on failure, this method doesn't exist

I'm not very keen on doing the work in JavaScript, but if I understand correctly, the analogue to what I want may look like something like this, as a JS function run inside a transaction (using Java driver, but specifying JS function--hardcoded, no params):

String action = "function (params) { "
        + "var graph_module = require('@arangodb/general-graph');"
        + "var graph = graph_module._graph('myGraph');"
        + "graph.collection1.save({name: 'a'});"
        + "graph.collection1.save({name: 'b'});"
        + "graph.collection2.save({name: 'c'});"
        // ...so on...  
        + "}";

db.transaction(action, String.class, options); // request through Java driver

I may want to mix in some other edge updates, deletes--all done at a high level through the graph module--in a single transaction. My question is, can I get what I want, and is this the right way?

I haven't tried the above JS transaction, yet, but I'm looking for guidance before darting down blind alleys.

Upvotes: 1

Views: 228

Answers (1)

mpv89
mpv89

Reputation: 1891

Unfortunately there is - currently - no other way than creating a server-side transaction with javascript. Your second code sample is currently the right and only way.

But there will be client-side transactions within one of the next major releases (3.x) of ArangoDB. Then you should be able to work like in your first code sample.

Upvotes: 2

Related Questions