BrandNewStory
BrandNewStory

Reputation: 71

Transaction close/commit issue in Neo4j Java

I'm new to Neo4j. I'm writing a java program using Neo4j database, and I met an issue when I create a node.

I use one transaction for creating the new node, and then I want to print its id, using node.getProperty(ID). But I got error message as attached below. So my question is, when using transaction in java, when should we create and commit/close it? Do we need to create a new transaction and commit/close it each time when creating or searching a node?

Exception in thread "main" org.neo4j.graphdb.NotInTransactionException: The transaction has been closed.
    at org.neo4j.kernel.impl.coreapi.TransactionImpl.checkInTransaction(TransactionImpl.java:695)
    at org.neo4j.kernel.impl.coreapi.TransactionImpl.kernelTransaction(TransactionImpl.java:576)
    at org.neo4j.kernel.impl.core.NodeEntity.getProperty(NodeEntity.java:428)

Update: My original transaction code is like this

Node n;
try(Transaction tx = graphDB.beginTx(){
   n = tx.createNode(label);
   n.setProperty(PROPERTY,value);
   tx.commit();
}
System.out.println(n.getProperty(PROPERTY));

Now I moved the print line to the try clause so it's in the same transaction.

Upvotes: 0

Views: 802

Answers (1)

cybersam
cybersam

Reputation: 66957

A Node object provided by a transaction is only valid for the life of the transaction. If you want to use the value of a node property outside of the transaction, you should get the value and store it someplace suitable during the transaction.

Upvotes: 1

Related Questions