Node.js pg promise是否有办法触发赢得';不影响外部交易?
在Node.js pg promise是否有办法触发赢得';不影响外部交易?,node.js,postgresql,transactions,pg-promise,Node.js,Postgresql,Transactions,Pg Promise,在pg promise中,我需要触发一个内部事务,该事务可以在需要时回滚,而不会在出错时导致调用事务回滚: var db = pgp()(connection); db.task( function (tk){ tk.method(/* Logic used to decide if I need to continue */) .then( function(data){ if (!data) return; tk.tx( function*(tx){ //
pg promise
中,我需要触发一个内部事务,该事务可以在需要时回滚,而不会在出错时导致调用事务回滚:
var db = pgp()(connection);
db.task( function (tk){
tk.method(/* Logic used to decide if I need to continue */)
.then( function(data){
if (!data) return;
tk.tx( function*(tx){
// Somewhere in here I need to fire another transaction that I don't care if it fails
// but I need things to roll back inside of it should it fail
// without cause this tx to fail
})
})
})
到目前为止,我所尝试的一切都只会导致外部事务(tx
)回滚,如果内部事务失败,而不是内部事务回滚,并且外部事务继续执行后面的逻辑。如果子事务失败,是否有可靠的方法使内部事务不会导致tx
回滚
另外:当我尝试使用一个Bluebird
承诺时,这也会失败。一些(tx1,tx2)
失败会导致tx
回滚,而另一个tx也会失败并回滚。正如名字所暗示的,其中的一切都建立在承诺之上,包括事务逻辑,因此,您正在寻找的答案完全与承诺相关:
如果您不希望内部承诺的结果影响外部承诺,那么您只是不将该内部承诺链接到父级,而是在本地处理/处理它
对于您的交易,这意味着:
tk.tx(function * (t1) {
return yield t1.tx(function * (t2))
// chained inner transaction (savepoint)
});
}).then(data=>{}).catch(error=>{});
你应该这样做:
tk.tx(function * (t1) {
t1.tx(function * (t2))
// unchained/localized inner transaction (savepoint)
}).then(data=>{}).catch(error=>{});
}).then(data=>{}).catch(error=>{});
i、 e.您在本地处理内部事务的结果,而不将其链接到父事务。我很好奇这是否是因为我们被锁定在了pg promise的旧版本中。我将检查github上的提交历史记录以及可能的解决方案。@RobertMennell可能是您在中间混淆了ES6生成器,这些生成器可能很棘手,很容易出错,您应该或不应该使用
yield
;)谢谢你指出这一点!我没想到,但这对我之后的每个人都有帮助