Subbu
Subbu

Reputation: 13

Getting NotInTransactionException while querying neo4j index

I am currently using neo4j 1.8.1 . I am getting NotInTransactionException , when I am querying the neo4j index to get some nodes.

Following is a simple query , which i am executing on neo4j

 if (graphDb.index().existsForNodes("NODEINDEX")) {
  IndexHits<Node> hits = graphDb.index().forNodes(NODEINDEX).query(query);
}

The following is stacktrace for the exception.

"message" : "Error fetching transaction for current thread",
"exception" : "NotInTransactionException",
"stacktrace" : [ "org.neo4j.kernel.impl.index.IndexConnectionBroker.getCurrentTransaction(IndexConnectionBroker.java:134)", "org.neo4j.kernel.impl.index.IndexConnectionBroker.acquireReadOnlyResourceConnection(IndexConnectionBroker.java:84)", "org.neo4j.index.impl.lucene.LuceneIndex.getReadOnlyConnection(LuceneIndex.java:105)", "org.neo4j.index.impl.lucene.LuceneIndex.query(LuceneIndex.java:245)", "org.neo4j.index.impl.lucene.LuceneIndex.query(LuceneIndex.java:227)", "org.neo4j.index.impl.lucene.LuceneIndex.query(LuceneIndex.java:238)", "com.uprr.netcontrol.starmap.neo4j.plugins.aggregate_node_status.NodeStatusHelper.getGraphNodes(NodeStatusHelper.java:39)", 

I found the following in Neo4j api.

 private Transaction getCurrentTransaction() throws NotInTransactionException
{
    try
    {
        return transactionManager.getTransaction();
    }
    catch ( SystemException se )
    {
        throw new NotInTransactionException(
                "Error fetching transaction for current thread", se );
    }
}

Do we need to explicitly start a transaction for querying neo4j index?
Any thoughts?
Thanks

Upvotes: 1

Views: 507

Answers (2)

whiterook6
whiterook6

Reputation: 3544

if there isn't an existing appropriate index, I think it'll create one before returning it; this operation needs to be wrapped in a transaction.

Upvotes: 0

Mattias Finn&#233;
Mattias Finn&#233;

Reputation: 3054

Here's a theory: I don't know if this is only an issue with the code pasted here but the check:

if (graphDb.index().existsForNodes("NODEINDEX"))

checks for the index named "NODEINDEX", however the actual query

graphDb.index().forNodes(NODEINDEX).query(query);

checks for the index named whatever is in the constant NODEINDEX. Those two are probably not the same and so it tries to create that index for you and fails due to not being in a transaction.

Upvotes: 1

Related Questions