typeorm-transactional-cls-hooked icon indicating copy to clipboard operation
typeorm-transactional-cls-hooked copied to clipboard

Transactional not rollback excute Promise.all Propagation.REQUIRED

Open x302502 opened this issue 4 years ago • 1 comments

I have a function that uses

@Transactional() excute() { ... const promises = []; promises.push(this.receiptDetailRepo.save(listReceiptDetail)) promises.push(this.lotattributeRepo.insert(listLotattribute)) promises.push(this.lotxlocxidRepo.save(listIvUpdate)) promises.push(this.lotxlocxidRepo.insert(listIvInsert)) promises.push(this.itrnRepo.insert(listItrn)) promises.push(Promise.reject("ERROR")); const [resReceiptDetails] = await Promise.all(promises); }

but it doesn't rollback;

What I am missing?

x302502 avatar Jun 16 '21 10:06 x302502

This is a common gotcha.

Promise.all() rejects as soon as the first promise gets rejected, triggering a rollback. If other operations referencing the transaction are still pending, they get orphaned and executed outside the transaction, making it appear as if the rollback did not happen.

You need to use something like Promise.allSettled() instead, and throw if any promise failed.

svvac avatar Aug 05 '22 10:08 svvac