Last active
December 10, 2015 04:48
-
-
Save mikesname/4383736 to your computer and use it in GitHub Desktop.
Blueprints nested transaction handling, contrived example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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