为什么Node-Neo4j没有正确设置数据?

想象一下这样的查询:

match (i:QuestionOrder) set i.count=i.count+1 merge (q:Question {text: '+text here+', index: i.count}) return q 

Neo4j保证写入locking,如果集合发生在node-neo4j中同一个查询所隐含的相同事务中。 但是,我得到以下输出:

 [ { "columns":["q"], "data":[{"text":"Have Kids...","index":1,"_id":542}] }, { "columns":["q"], "data":[{"text":"You are...","index":1,"_id":545}] } ] 

从我的理解来看,锁应该防止index相同。 我在这里错过了什么? 我该如何解决这个问题?

可能它有助于并发工作来改变这个查询来添加一个额外的testing。 如果您有两个并发事务,则locking将在设置操作中获取。 所以两者都已经读取了i.count并且正在等待相同的值。

所以要么你早一点抓住那个锁:

 match (i:QuestionOrder) set i.lock = NOT i.lock set i.count=i.count+1 merge (q:Question {text: '+text here+', index: i.count}) return q 

或者你添加一个额外的检查,避免合并发生(那么你必须重试)

 match (i:QuestionOrder) WITH i, i.count + 1 as new_count set i.count = new_count WITH i, new_count WHERE i.count = new_count merge (q:Question {text: '+text here+', index: i.count}) return q 

或者在第一个locking的同一个tx中发送两个不同的语句。