0

I am trying to select a path with locking the last node in that path using Java OGM for Neo4j.

To do that in cypher I have written the following query:

String q = "Match path = (p:Root) - [*1..100]-(m:Leaf) WHERE m.State = 'Non-Processed' WITH m,p,path ORDER BY length(path) Limit 1 SET m.State = 'Processing' RETURN path"

It selects the necessary path with locking the last leaf(by changing its State property).

However, when I try to execute this query:

session.query(Path.class, q, propertyMap)

I get a java.lang.RuntimeException: query() only allows read only cypher. To make modifications use execute()

What is the proper way to do this?

Vahagn
  • 386
  • 2
  • 21
  • did you try `execute` method instead of `session.query()`? – sidgate Dec 29 '15 at 11:30
  • `Method threw 'java.lang.RuntimeException' exception.: execute() must not return data. Use query() instead.` – Vahagn Dec 29 '15 at 11:32
  • then do not return anything from the query, remove `RETURN path` – sidgate Dec 29 '15 at 11:34
  • There are multiple threads performing the same query and processing paths. In order to avoid multiple therads to process the same path I am trying to implement this mechanism of locking. – Vahagn Dec 29 '15 at 11:37

2 Answers2

1

You're probably using an older version of neo4j-ogm which had the restriction on session.query(). Please upgrade to neo4j-ogm 1.1.4

Luanne
  • 19,145
  • 1
  • 39
  • 51
0

Found a (probably not the best) solution.

String uid = UUID.randomUUID().toString();
String lockQuery = "Match path = (p:Root) - [*1..100]-(m:Leaf)"
   + "WHERE m.State = 'Non-Processed' "
   + "WITH m,p,path ORDER BY length(path) Limit 1 SET m.lock = " + uid
session.execute(lockQuery);
String getQuery = "Match path = (p:Root) - [*1..100]-(m:Leaf)"
   + "WHERE m.lock = " + uid + "RETURN path";
Path path = session.query(Path.class, getQuery, new Hashmap<String, Object>());

Will this work?

Vahagn
  • 386
  • 2
  • 21