Closed preterer closed 3 years ago
Silly me. It's solvable by putting Propagation.MANDATORY
on reprodC
, and creating a reprodD
which just calls reprodC
in a started transaction, to use it in places where you can't start a transaction (like tests, or cli).
@Transactional()
public reprodD(id: number): Promise<any> {
return this.reprodC(id);
}
@Transactional({ propagation: Propagation.MANDATORY })
public async reprodC(id: number): Promise<any> {
let entity = await this.repository.findOne(id);
if (!entity) {
entity = this.repository.create();
}
entity.name = 'Test';
await this.repository.save(entity);
}
I double checked https://github.com/odavid/typeorm-transactional-cls-hooked/issues/58 and despite the fact, that I still belive the code was not written correctly, there actually is a problem with
Promise.all
calls if there are many nested calls to methods decorated as Transactional.Reproduction code (needs
sql.js
library installed):I'm not really sure what can you do with that in your library, as there is no way of canceling a promise.
A workaround I found is to catch the errors, let all the promises continue, and then throw the caught error when they're all finished, It is used in the
workaround
method ofTestService
.However it is ugly and consumes time/server power for no reason, and would be great if there was a way of solving this issue in the library. Maybe it would be possible to mark the transaction as failed in CLS and just throw errors on any repository call if it's marked, I'll try to look into it.
Anyway, untill it's resolved I'll leave this issue here with a workaround.