I've experienced an issue regarding TypeORM transactional entity manager. Below is the code snippet.
class Fizz extends BaseEntity {
...
buzzId: number;
}
class Buzz extends BaseEntity { ... }
class FizzService {
createFizz(input) {
return Fizz.create(...input);
}
}
class BuzzService {
createBuzz(input) {
return Buzz.create(...input);
}
}
class FizzResolver {
async generate(payload) {
await dataSource.transaction(
async (transactionalEntityManager) => {
let fizz = fizzService.createFizz(payload.fizzInput);
const buzz = buzzService.createBuzz(payload.buzzInput);
await transactionalEntityManager.save(buzz); // sometimes persists the data, but sometimes not
fizz = Object.assign(fizz, { buzzId: buzz.id });
await transactionalEntityManager.save(fizz);
}
);
}
}
Given this code, I was expecting that if there were any error a rollback would happen which would not commit anything. Anyway, what happens is sometimes the Buzz entity record is not persisted but I still have the generated id in the Fizz entity record.
Any idea on why?