Ejemplo n.º 1
0
    public async Task Should_Rollback_Transaction_Manually_With_Double_DbContext_Transaction()
    {
        var personId = Guid.NewGuid();
        var bookId   = Guid.NewGuid();

        using (var scope = ServiceProvider.CreateScope())
        {
            var uowManager = scope.ServiceProvider.GetRequiredService <IUnitOfWorkManager>();

            using (uowManager.Begin(new AbpUnitOfWorkOptions {
                IsTransactional = true
            }))
            {
                _unitOfWorkManager.Current.ShouldNotBeNull();

                await _personRepository.InsertAsync(new Person(personId, "Adam", 42));

                await _bookRepository.InsertAsync(new BookInSecondDbContext(bookId, bookId.ToString()));

                await _unitOfWorkManager.Current.SaveChangesAsync();

                //Will automatically rollback since not called the Complete!
            }
        }

        (await _personRepository.FindAsync(personId)).ShouldBeNull();
        (await _bookRepository.FindAsync(bookId)).ShouldBeNull();
    }
Ejemplo n.º 2
0
    public async Task Should_Rollback_Transaction_When_An_Exception_Is_Thrown()
    {
        var          personId         = Guid.NewGuid();
        const string exceptionMessage = "thrown to rollback the transaction!";

        try
        {
            await WithUnitOfWorkAsync(new AbpUnitOfWorkOptions { IsTransactional = true }, async() =>
            {
                await _personRepository.InsertAsync(new Person(personId, "Adam", 42));
                throw new Exception(exceptionMessage);
            });
        }
        catch (Exception e) when(e.Message == exceptionMessage)
        {
        }

        var person = await _personRepository.FindAsync(personId);

        person.ShouldBeNull();
    }