Skip to content

Instantly share code, notes, and snippets.

@mikesname
Last active December 10, 2015 04:48
Show Gist options
  • Save mikesname/4383736 to your computer and use it in GitHub Desktop.
Save mikesname/4383736 to your computer and use it in GitHub Desktop.
Blueprints nested transaction handling, contrived example.
import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph;
public class BlueprintsTest {
private final String[] testData = {
"foo",
"bar",
null, // Oops! Null property
"baz"
};
private final Neo4jGraph graph;
public BlueprintsTest() {
graph = new Neo4jGraph("test-graph" + (new java.util.Date()).getTime());
}
public void createThing(String value) throws Exception {
try {
Vertex vertex = graph.addVertex(null);
vertex.setProperty("key", value);
graph.stopTransaction(Conclusion.SUCCESS);
} catch (Exception e) {
graph.stopTransaction(Conclusion.FAILURE);
throw e;
}
}
public void createBatch() throws Exception {
try {
for (String t : testData) {
createThing(t);
}
graph.stopTransaction(Conclusion.SUCCESS);
} catch (Exception e) {
graph.stopTransaction(Conclusion.FAILURE);
throw e;
}
}
public int countNodes() {
int count = 0;
for (Vertex _ : graph.getVertices()) count++;
return count;
}
public static void main(String[] args) {
BlueprintsTest testRunner = new BlueprintsTest();
try {
testRunner.createBatch();
} catch (Exception e) {
System.err.println("Caught exception: " + e.getMessage());
}
System.out.println("Nodes in graph: " + testRunner.countNodes());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment