有没有办法在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){ // 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 Promise.some(tx1, tx2)作为失败时导致tx回退,而另一个tx#失败并回滚,这也失败了。

顾名思义, pg-promise中的所有东西都build立在承诺上,包括事务逻辑,所以你要寻找的答案纯粹是承诺相关的:

如果你不希望内部承诺的结果影响外部承诺,那么不要将内部承诺链接到父级,而是在本地处理/处理它。

对于您的交易,这意味着,而不是:

 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=>{}); 

即你在本地处理来自内部事务的结果,而不把它链接到父项。