MATCH (q:Question {id: "q1"})
CALL {
WITH q
OPTIONAL MATCH (q)-[:HAS]->(a:Answer)
DETACH DELETE a
}
CREATE (a:Answer)
SET a = {id: "a1", content: 25}
CREATE (q)-[:HAS]->(a)
This is a simple query which updates the answer related to a question. So when there is a new answer to be added to a question, it will remove existing answer first and then create a new relation between the question and new answer. When we run it one at a time there is no issues, but when we run it multiple times in parallel, it is behaving differently. It just creates more than one relations.
CALL apoc.periodic.iterate(
"UNWIND range(0, 5) AS i RETURN i",
"MATCH (q:Question {id: 'q1'})
CALL {
WITH q
OPTIONAL MATCH (q)-[:HAS]->(a:Answer)
DETACH DELETE a
}
CREATE (a:Answer)
SET a = {id: i, content: 25}
CREATE (q)-[:HAS]->(a)",
{batchSize:10000, parallel:true})
So here we tried executing same query multiple times and it eventually created Answers for the same Question. So doesn't it lock when we create a node / delete? How does it lock the Question node here? When does it release the lock for Question?