Aliheym / typeorm-transactional

A Transactional Method Decorator for TypeORM that uses Async Local Storage or cls-hooked to handle and propagate transactions between different repositories and service methods.
MIT License
201 stars 27 forks source link

When I use try-catch, the transaction doesn't work #38

Closed kshlove735 closed 10 months ago

kshlove735 commented 1 year ago
@injectable()
export class ImageService {
  constructor(
    private imageRepository: ImageRepository,
    private labellingRepository: LabellingRepository,
  ) { }

  @Transactional()
  async testImageUpload(imageEntities, labellingEntities ) {

    try {
      let imagesCreated: Image[] = await this.imageRepository.save(imageEntities);
      await this.labellingRepository.save(labellingEntities);
      return imagesCreated;

    } catch (e) {
      console.log(e);
    }
  }
}

When I use try-catch, the transaction doesn't work, but when I don't use try-catch, it works. How can I use exception handling?

odai-alali commented 1 year ago

The transactional method executes rollback when an error is thrown in it. You should throw errors in your service and handle them in the controller.