2017-09-14 90 views
2

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回滚?

作为补充:当我尝试使用BluebirdPromise.some(tx1, tx2)作为失败原因tx回滚而其他tx#失败并回滚时也失败。内pg-promise

回答

2

一切,顾名思义,建在承诺,包括事务逻辑,所以你正在寻找的答案是纯粹的承诺有关:

如果你不希望内部的结果承诺会影响外部承诺,您只需将内部承诺链接到父级,就可以在本地处理/处理它。

为您的交易,这意味着不是这样:

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

即你在本地处理从内部交易的结果,如果没有它链接到父母。

+0

我很好奇,如果是因为我们被锁定到pg-promise的旧版本。我将检查github上的提交历史记录以及可能的解决方案。 –

+1

@RobertMennell可能是因为你在混合使用ES6生成器,那些可能会很棘手,容易犯错误,你应该或不应该使用'yield';) –

+0

谢谢指出!没有想到这一点,但对每个追随我的人都会有所帮助 –

相关问题