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

Add an article to explain transaction in UOW. #21994

Open
maliming opened this issue Jan 24, 2025 · 2 comments · May be fixed by #21998
Open

Add an article to explain transaction in UOW. #21994

maliming opened this issue Jan 24, 2025 · 2 comments · May be fixed by #21998

Comments

@maliming
Copy link
Member

The current ABP official documentation still lacks sufficient explanation regarding uow.CompleteAsync, which has caused a lot of confusion during development.

It would be greatly beneficial for all developers if the documentation could provide detailed explanations or examples about the effects of SaveChangesAsync, autoSave behavior of repositories, uow.CompleteAsync, etc., when uow.isTransactional is set to either true or false.

Thank you to the ABP official team for your efforts.

(By the way, I am a ABP Commercial user. Should suggestions like this be posted here or in the official support forum?)

Originally posted by @AlanLaiTw in #12717

@maliming maliming added this to the 9.2-preview milestone Jan 24, 2025
@maliming maliming self-assigned this Jan 24, 2025
@maliming maliming changed the title Add an article to explain UOW. Add an article to explain transaction in UOW. Jan 24, 2025
maliming added a commit that referenced this issue Jan 24, 2025
@maliming maliming linked a pull request Jan 24, 2025 that will close this issue
@AlanLaiTw
Copy link

@maliming
Thank you so much for organizing this document so quickly, efficiently, and comprehensively! It’s incredibly helpful!


I’ve been testing the behavior and effects of UnitOfWork over the past few days. Here, I’d like to provide some additional suggestions for the document (and also confirm and verify whether my understanding is correct).


Ways to Rollback (when manually controlling UnitOfWork)

When implementing complex features, it may be necessary to carefully manage the timing of rollbacks. My understanding is as follows:

  1. Manual Rollback
    You can call uow.RollbackAsync() at any point when needed to immediately rollback the current transaction of the UnitOfWork.

  2. Automatic Rollback
    When the UnitOfWork is disposed, if uow.CompleteAsync() has not been called, a rollback is automatically triggered.

Therefore, the rollback timing differs in the following two scenarios:

Example 1

using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
{
    try
    {
        await _bookRepository.UpdateAsync(book);
        await _productRepository.UpdateAsync(product);

        await uow.CompleteAsync();
    }
    catch (Exception)
    {
        // ❗ Important ❗: If an exception occurs and enters the catch block, 
        // the UnitOfWork's using block has not yet ended (Dispose has not been called), 
        // so rollback has not been triggered yet.


        // Do something when an exception is thrown
        throw;
    }
    finally
    {
        // ❗ Important ❗: At this point, the UnitOfWork's using block has not yet ended (Dispose has not been called),
        // so rollback has not been triggered yet.


        // Do something in finally block
    }
}

Example 2

try
{
    using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
    {
        await _bookRepository.UpdateAsync(book);
        await _productRepository.UpdateAsync(product);

        await uow.CompleteAsync();
    }
}
catch (Exception)
{
    // If an exception occurs and enters the catch block, 
    // the UnitOfWork's using block has already ended (Dispose has been called),
    // so rollback has already been completed.


    // Do something when an exception is thrown
    throw;
}

uow.CompleteAsync() and uow.RollbackAsync() in UnitOfWork Can Only Be Called Once

Within a single UnitOfWork, both uow.CompleteAsync() and uow.RollbackAsync() will have no effect if called more than once.


If there are any misunderstandings, please feel free to correct me.
Thank you so much!

@maliming
Copy link
Member Author

maliming commented Jan 25, 2025

Thank you for reminding me, I forgot the rollback section.

Here is the latest article: https://github.com/abpframework/abp/blob/Understanding-Transactions-in-ABP-Unit-Of-Work/docs/en/Community-Articles/2025-01-24-Understanding-Transactions-in-ABP-Unit-Of-Work/POST.md

Enjoy your commercial development, never rollback. :)

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

Successfully merging a pull request may close this issue.

2 participants