JasperFx / marten

.NET Transactional Document DB and Event Store on PostgreSQL
https://martendb.io
MIT License
2.81k stars 445 forks source link

UpdateRevision with SaveChangesAsync generates a ConcurrencyException #3284

Closed ivanoterrivel2 closed 3 months ago

ivanoterrivel2 commented 3 months ago

I want to use SaveChangesAsync after calling the UpdateRevision method.

On Marten solution, in numeric_revisioning.cs test files, for the public async Task optimistic_concurrency_failure_with_update_revision() If I change the SaveChanges() to use SaveChangesAsync() it throws a Marten.Exceptions.ConcurrencyException. The tests with UpdateRevision seem to fail when used with SaveChangesAsync();

[Fact]
public async Task optimistic_concurrency_failure_with_update_revision()
{
    var doc1 = new RevisionedDoc { Name = "Tim" };
    theSession.Store(doc1);
    theSession.SaveChanges();

    doc1.Name = "Bill";
    theSession.Store(doc1);
    theSession.SaveChanges();

    doc1.Name = "Dru";
    theSession.Store(doc1);
    theSession.SaveChanges();

    var doc2 = new RevisionedDoc { Id = doc1.Id, Name = "Wrong" };
    theSession.UpdateRevision(doc2, doc1.Version + 1);
    await theSession.SaveChangesAsync();

    theSession.Logger = new TestOutputMartenLogger(_output);

    await Should.ThrowAsync<ConcurrencyException>(async () =>
    {
        theSession.UpdateRevision(doc2, 2);
        theSession.SaveChanges();
    });
}

Stack Trace:  AutoClosingLifetime.ExecuteBatchPagesAsync(IReadOnlyList1 pages, List1 exceptions, CancellationToken token) line 347 AutoClosingLifetime.ExecuteBatchPagesAsync(IReadOnlyList1 pages, List1 exceptions, CancellationToken token) line 369 AutoClosingLifetime.ExecuteBatchPagesAsync(IReadOnlyList1 pages, List1 exceptions, CancellationToken token) line 369 <b__2_0>d.MoveNext() --- End of stack trace from previous location --- Outcome1.GetResultOrRethrow() ResiliencePipeline.ExecuteAsync[TState](Func3 callback, TState state, CancellationToken cancellationToken) DocumentSessionBase.ExecuteBatchAsync(IUpdateBatch batch, CancellationToken token) line 192 ExceptionTransformExtensions.TransformAndThrow(IEnumerable`1 transforms, Exception ex) DocumentSessionBase.ExecuteBatchAsync(IUpdateBatch batch, CancellationToken token) line 199 DocumentSessionBase.ExecuteBatchAsync(IUpdateBatch batch, CancellationToken token) line 216 DocumentSessionBase.SaveChangesAsync(CancellationToken token) line 99 numeric_revisioning.optimistic_concurrency_failure_with_update_revision() line 173 --- End of stack trace from previous location ---