Vahagn
Vahagn

Reputation: 426

Neo4j Java OGM select with lock

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?

Upvotes: 0

Views: 158

Answers (2)

Luanne
Luanne

Reputation: 19373

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

Upvotes: 1

Vahagn
Vahagn

Reputation: 426

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?

Upvotes: 0

Related Questions