odavid / typeorm-transactional-cls-hooked

A Transactional Method Decorator for typeorm that uses cls-hooked to handle and propagate transactions between different repositories and service methods. Inpired by Spring Trasnactional Annotation and Sequelize CLS
MIT License
522 stars 86 forks source link

Transactional not rollback excute Promise.all Propagation.REQUIRED #100

Open x302502 opened 3 years ago

x302502 commented 3 years ago

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?

svvac commented 2 years ago

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.