在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#也会失败和回滚。
发布于 2017-09-14 22:48:00
顾名思义,pg-承诺中的所有内容都建立在承诺之上,包括事务逻辑,所以您要寻找的答案纯粹是与承诺相关的:
如果您不希望内部承诺的结果影响外部承诺,则只需将内部承诺链接到父方,而是在本地处理/处理它。
对于您的事务,这意味着不是这样:
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=>{});也就是说,您在本地处理来自内部事务的结果,而不将其链接到父事务。
https://stackoverflow.com/questions/46229132
复制相似问题