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
524 stars 86 forks source link

Transaction not rolling back as expected #118

Open sayinmehmet47 opened 10 months ago

sayinmehmet47 commented 10 months ago

I'm using typeorm-transactional-cls-hooked in my NestJS application to manage transactions. I have a method decorated with @Transactional() where I'm trying to delete a non-existing record to force a transaction rollback. However, even though I see a log message indicating that the transaction has been rolled back, the changes made earlier in the transaction are not being rolled back in the database.

image

Expected behavior I expect that when an error is thrown in a transaction, all changes made in that transaction are rolled back. To Reproduce Here's a simplified version of my code:

@Transactional()
async updateTask({ id }: Task): Promise<TaskEntity> {
  // ... some code to update a task ...

  const taskSaved = await this.taskRepository.save(updatedTask);

  runOnTransactionRollback(() => {
    this.logger.log('Transaction rolled back');
  });

  // delete a task that does not exist to test transaction rollback
  await this.taskRepository.delete({ id: 'non-existing-id' });

  return taskSaved;
}

In this code, I'm updating a task and then trying to delete a non-existing task. When the delete operation doesn't find a task to delete, it should throw an error and the transaction should roll back, undoing the previous save operation. However, the save operation is not being rolled back.

Environment:

Node.js version: (e.g., 14.15.1) NestJS version: (e.g., 7.6.15) typeorm-transactional-cls-hooked version: (e.g., 1.0.0) Database and version: (e.g., PostgreSQL 13.3)