method findNodes in GraphDatabaseService throws MultipleFoundException

30 views
Skip to first unread message

David Starkweather

unread,
Jun 15, 2016, 8:41:49 PM6/15/16
to Neo4j
Hell all, 

Thanks for all your hard work in delivering such a great product. 

I have an application built on top of neo4j using the java api for v3.0.1. It requires finding a "top" node first at the beginning of every one of my 
graph operations.  This top node serves as the start of my graph db.  I noticed an earlier edition of neo4j used a 
reference node from which to retrieve the graph.  Since that apparently is no longer a feature in newer releases, 
I chose to label this top node with a "TOP" label, and then simply retrieve this node using findNode method.  Now, this method works 
great so long as the testing is done in a single thread.  However, when used in multiple threads, it occasionally gets a 
MultipleFoundException thrown when invoking this findNode method.  I feel quite certain that my code only creates one top 
node at a time, and whenever it creates a new top node, it deletes the old top node and always does so within a single transaction block.  
However, if findNodes() is used instead, I find that two nodes can be counted by iterating through the ResourceIterator (with hasNext/getNext). 
(Although I'm not sure if they are both viable active nodes.) 

Is this the best technique for maintaining such a single top node?  Or is there a more preferred way?

Does the ResourceIterator that is returned ever contain outdated entries? (I notice the javadocs for this class is missing.) 

I would appreciate any ideas you may have on this.  

Thanks again.
David Starkweather

Michael Hunger

unread,
Jun 15, 2016, 8:53:20 PM6/15/16
to ne...@googlegroups.com
You have to create a lock to make that an exclusive operation.

Best with cypher's MERGE and a unique constraint which take care of creating that index lock for you:

create constraint on t:Top assert t.id is unique;

MERGE (t:Top {id:123}) RETURN t;


--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Starkweather

unread,
Jun 15, 2016, 10:53:46 PM6/15/16
to ne...@googlegroups.com
Thanks, Michael.  I'm using an embedded graph database with the java api.  I think I fixed it with the "synchronized" key word on the method. 
Reply all
Reply to author
Forward
0 new messages