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

How to rollback the database transaction when there is exception? #22347

Open
raine93 opened this issue Mar 12, 2025 · 1 comment
Open

How to rollback the database transaction when there is exception? #22347

raine93 opened this issue Mar 12, 2025 · 1 comment

Comments

@raine93
Copy link

raine93 commented Mar 12, 2025

In my onPostAsync():

try {

Call _appService.UpdateAsync()
2. _appService.UpdateAsync do the validation check (using Check.Positive, etc) before calling the _repository.UpdateAsync (no try catch inside)
}
catch (Exception ex) {
//set notification message
notificationmessage = "Fail to update";
}

return RedirectToPage();

The Check.Positive() throw an exception which goes to my catch Exception, set my notificationmessage and redirect to my page. But the database is still being updated instead of rolling back.

I read from here: https://abp.io/docs/latest/framework/architecture/domain-driven-design/unit-of-work
if it does not throw exception, it will commit the transaction. But now that it hit my catch Exception and I am trying to return the error with my own notification, it still commit the transaction.

I try commented out the _repository.UpdateAsync() and it still can update the database (?).
I also try to disable the unit of work with this

Configure<AbpUnitOfWorkDefaultOptions>(options =>
{
    options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled;
});

but it still does not work.

How do I make it follow my steps (eg. like if i commented out the _repository.UpdateAsync(), it should not do any update to database) and disable this current behaviour? Thanks.

@realLiangshiwei
Copy link
Member

https://abp.io/docs/latest/framework/architecture/domain-driven-design/unit-of-work#the-current-unit-of-work

You can call rollback yourself.

await _unitOfWorkManager.Current.RollbackAsync();

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

2 participants