question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with RollbackToSavepointAsync not working after a DbCommand failure due to Geography column (NetTopologySuite)

See original GitHub issue

File a bug

We are wrapping calls to the DbContext SaveChangesAsync with our own transaction, savepoint and rollback. We noticed that when a command failure happened the rollback would not work due to the system thinking that transaction was no longer available.

internal class ReadWriteUnitOfWork : UnitOfWork, IReadWriteUnitOfWork
    {
        private const string SavepointName = nameof(ReadWriteUnitOfWork) + "Savepoint";
        private readonly IEntityCacheFacade _entityCache;
        private IDbContextTransaction? _transaction;
        private bool _disposed;

        public ReadWriteUnitOfWork(
            UnitOfWorkContext unitOfWorkContext,
            DbContext dbContext,
            IEntityCacheFacade entityCache)
            : base(unitOfWorkContext, dbContext)
        {
            _entityCache = entityCache;
        }

        public async Task SaveChangesAsync()
        {
            var dbContext = DbContext;
            _transaction ??= await dbContext.Database.BeginTransactionAsync();

            await _transaction.CreateSavepointAsync(SavepointName);
            try
            {
                await dbContext.SaveChangesAsync(false);
                await _entityCache.UpdateEntriesAsync(dbContext.ChangeTracker.Entries());
            }
            catch
            {
                await _transaction.RollbackToSavepointAsync(SavepointName);
                throw;
            }
            finally
            {
                await _transaction.ReleaseSavepointAsync(SavepointName);
            }

            // Accept changes after successfully updating cache.
            dbContext.ChangeTracker.AcceptAllChanges();
        }

   ....
}

Application insights show the timeline when we are attempting to save changes on a known sql command failure: image You can see that both the Save Transactions (the one we are doing and the one that efcore is doing) work properly. Then the sql db command fails. Following that both of the transaction rollbacks fail with this error: The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.

When I attempt to dive into it, I noticed that when Creating the Savepoint the _dbTransaction in the RelationalTransction has both a set Connection and a InternalTransaction object.

image

After it calls SaveChangesAsync when I look at that same RelationTransaction, the fields on the _dbTransation have been set to null.

image

This happens to the __EFSavePoint as well as the one we have wrapped around it.

Has anyone else experienced this problem or have any ideas about what could cause it?

SQLServer Multiple Active Result Sets is not turned on.

Include provider and version information

EF Core version: 5.0.1 Database provider: Microsoft.EntityFrameworkCore.SqlServer - 5.0.1 Target framework: .NET 5.0 Operating system: WINDOWS 10 IDE: Rider 2020.3

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Sep 7, 2021

(got back to this after a while)

Well actually if you fetch “tx.SupportsSavepoints” it says “False”.

@cheenamalhotra that’s true, but isn’t that because it always returns false? 😃 AFAIK SqlClient hasn’t overridden that property, which was introduced in .NET 5.0 as part of https://github.com/dotnet/runtime/issues/33397. I couldn’t find an issue tracking that, so I opened https://github.com/dotnet/SqlClient/issues/1256.

Stepping back… So far, the only scenario we know about where this happens is the above, which is the result of a programmer bug, rather than an actual exception that is expected to happen at runtime. I think it’s OK to not do anything until we have a more relevant runtime repro - clearing milestone to bring to triage on the EF side.

0reactions
ajcvickerscommented, Sep 11, 2021

Note from triage: no-action on the EF side for this case, since the exception is caused by bad binary data. However, it does seem like this may be an issue in other places (and other providers) if the transaction is reset by an exception.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework Core 3.1 with NetTopologySuite. ...
The exception I get on save is the following: SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found