我正在使用Ne04j v3.1.0和Java驱动程序。我无法在显式事务中获取更新语句(cypher SET)的结果以保持不变。事务中的返回值显示更改,但在事务结束后,更改似乎已消失。我知道更新对结果处理很敏感
[http://neo4j.com/docs/developer-manual/current/drivers/process-results/][1]
所以我在代码中积极主动地使用了所有结果并关闭了我的会话。
我写了一个测试程序来演示这个问题:
package uk.co.scapps.createdirs.neo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Transaction;
public class Trans {
static Driver driver = GraphDatabase.driver("bolt://localhost", AuthTokens.basic("neo4j", "neo4k"));
static Session session = driver.session();
final static Logger logger = LogManager.getLogger();
public static void main(String[] args) {
session = driver.session();
StatementResult results ;
session = driver.session();
try (Transaction tx = session.beginTransaction()) {
results = tx.run("MATCH (n:THING) RETURN n.name as name");
while (results.hasNext()) {
Record record = results.next();
System.out.println("in the txn before the update: " + record.get("name").toString());
}
results.consume();
results = tx.run("MATCH (n:THING {name: 'a'}) SET n.name = 'asdasd' RETURN n.name as name");
while (results.hasNext()) {
Record record = results.next();
System.out.println("results returned from the update: " + record.get("name").toString());
}
results.consume();
results = tx.run("MATCH (n:THING) RETURN n.name as name");
while (results.hasNext()) {
Record record = results.next();
System.out.println("after the update but still in the txn: " + record.get("name").toString());
}
results.consume();
tx.close();
session.close();
}
session = driver.session();
results = session.run("MATCH (n:THING) RETURN n.name as name");
while (results.hasNext()) {
Record record = results.next();
System.out.println("after the txn: " + record.get("name").toString());
}
results.consume();
session.close();
}
}
这是输出:
in the txn before the update: "a"
in the txn before the update: "b"
in the txn before the update: "c"
results returned from the update: "asdasd"
after the update but still in the txn: "asdasd"
after the update but still in the txn: "b"
after the update but still in the txn: "c"
after the txn: "a"
after the txn: "b"
after the txn: "c"
我没有包含代码,但我编写了一个非事务性(隐式事务)版本的程序,它按预期工作。
我将不胜感激。
答案 0 :(得分:1)
我未能执行txn.success()
。