Skip to content

Transaction not rolling back as expected #118

Open
@sayinmehmet47

Description

@sayinmehmet47

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions