Issue with RollbackToSavepointAsync not working after a DbCommand failure due to Geography column (NetTopologySuite)
See original GitHub issueFile 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:
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.
After it calls SaveChangesAsync
when I look at that same RelationTransaction, the fields on the _dbTransation have been set to null.
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:
- Created 2 years ago
- Comments:12 (11 by maintainers)
(got back to this after a while)
@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.
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.