Getting NotInTransactionException while querying neo4j index

↘锁芯ラ 提交于 2019-12-08 07:16:34

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/15521239/getting-notintransactionexception-while-querying-neo4j-index

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!