当我查看documentation for transactions时,我正在看这个例子:
var tx = session.beginTransaction();
tx.run("MERGE (bob:Person {name : {nameParam} }) RETURN bob.name AS name", {nameParam: 'Bob'})
.subscribe({
onNext: function (record) {
console.log(record.get('name'));
},
onCompleted: function () {
session.close();
},
onError: function (error) {
console.log(error);
}
});
//decide if the transaction should be committed or rolled back
var success = false;
if (success) {
tx.commit()
.subscribe({
onCompleted: function () {
// this transaction is now committed
},
onError: function (error) {
console.log(error);
}
});
} else {
//transaction is rolled black and nothing is created in the database
console.log('rolled back');
tx.rollback();
}
但是在上面的片段中它似乎没有改变success
某种方式我的意思是它如何确定事务是否成功执行或success
变量根本不会改变该值。
答案 0 :(得分:1)
这是因为您在session.close()
回调函数上调用onCompleted
。
当您关闭会话时,它将自动提交所有基础打开的交易。
此外,在您的示例中,您还没有等待tx.run
承诺完成。你应该在decide if the transaction should be committed or rolled back
所以你应该做那样的事情:
var driver = neo4j.v1.driver("bolt://localhost", neo4j.v1.auth.basic("neo4j", "neo4j"), { encrypted:false });
var session = driver.session();
// run statement in a transaction
var tx = session.beginTransaction();
tx.run("MERGE (bob:Person {name : {nameParam} }) RETURN bob.name AS name", {nameParam: 'Bob'})
.subscribe({
onNext: function (record) {
console.log(record.get('name'));
},
onCompleted: function () {
console.log('statement completed')
},
onError: function (error) {
console.log(error);
}
});
//decide if the transaction should be committed or rolled back
var success = true;
if (success) {
tx.commit()
.subscribe({
onCompleted: function () {
// this transaction is now committed
},
onError: function (error) {
console.log(error);
}
});
} else {
//transaction is rolled black and nothing is created in the database
console.log('rolled back');
tx.rollback();
}
session.close();
PS:我将联系开发团队以更新示例