Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction not rolling back as expected #118

Open
sayinmehmet47 opened this issue Dec 19, 2023 · 0 comments
Open

Transaction not rolling back as expected #118

sayinmehmet47 opened this issue Dec 19, 2023 · 0 comments

Comments

@sayinmehmet47
Copy link

sayinmehmet47 commented Dec 19, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant