javadatabaseneo4jgraph-databasesneo4j-java-api

Is it possible to determine if a `Node` is in a transaction in Neo4J using the Java API?


Is it possible to determine if a Node is in a transaction? It is possible to get a GraphDatabaseService by the method Node.getGraphDatabase.

I would like to do something like this:

public class Neo4JHelper {
    public void setProperty(Node node, String key, Object value) {
        if(isInTransaction(node) {
            node.setProperty(key, value);
        } else {
            throw new MyOwnException("You are trying to set a node outside a transaction... you suck");
        }
    }

    private boolean isInTransaction(Node node) {
        //Something
    }
}

The reason I want to do this is because I would like to give my users a custom error when trying to use my class Neo4JHelperoutside a transaction.

Another solution would be if it is possible to somehow tell the compiler that you need a transaction to use the method/class and otherwise give a compile error.


Solution

  • A node cannot be in a transaction, only a current execution (Thread) can be.

    There is an internal way to check for a running transaction:

       ThreadToStatementContextBridge txManager = ((GraphDatabaseAPI) graphDB).getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class);
       txManager.hasTransaction();