Database Reference
In-Depth Information
@Transactional
public Node createNodeViaAnnotatedMethod(String name, int age) {
Node node = gds.createNode();
node.setProperty("name", name);
node.setProperty("age", age);
return node;
}
public void setGraphDatabaseService(GraphDatabaseService gds) {
this.gds = gds;
}
}
You've seen how you can integrate Neo4j with a typical Spring application. For a more
object-oriented programming model, chapter 9 discusses Spring Data Neo4j, which
provides a mapping solution between domain classes and Neo4j.
Next, let's look at how you can add custom logic around Neo4j transactions using transac-
tion events.
7.4. Transaction events
Some databases implement triggers to enable custom code execution around database
transactions. Neo4j has transaction event handlers to fulfill this functionality.
The TransactionEventHandler interface contains three methods, beforeCom-
mit , afterCommit , and afterRollback , that when implemented and registered
with the database will be invoked at these key points for each transaction. The be-
foreCommit method can optionally return an object that will be passed on to the other
two methods if any context or state needs to be communicated.
The following listing shows the skeleton of a transaction event handler created from the
GraphDatabaseService object.
Listing 7.9. Transaction event handlers
graphDatabaseService.registerTransactionEventHandler(new
TransactionEventHandler<Object>() {
@Override
public Object beforeCommit(TransactionData data) throws Exception {
Search WWH ::




Custom Search