Null reference when calling SaveChangesAsync method using transaction scope - Distributed transaction - Oracle and MSSQL
public class DistributedTransaction{
OracleDbContext _oracleDbContext;
MsSqlDbContext _msSqlDbContext;
public DistributedTransaction(OracleDbContext oracleDbContext, MsSqlDbContext msSqlDbContext)
{
_oracleDbContext = oracleDbContext;
_msSqlDbContext = msSqlDbContext;
}
public async Task ExecuteAsync(){
using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
try {
//Do some Inserts updates
//Do some Inserts updates
//Do some Inserts updates
await _oracleDbContext.SaveChangesAsync(); //This works fine
await _msSqlDbContext.SaveChangesAsync() ; //Gets Null reference here
scope.Complete();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Transactions.TransactionStateAborted.EnterState(InternalTransaction tx)
at System.Transactions.TransactionStateDelegatedBase.EnterState(InternalTransaction tx)
at System.Transactions.EnlistableStates.Promote(InternalTransaction tx)
at System.Transactions.Transaction.Promote()
at System.Transactions.TransactionInterop.ConvertToOletxTransaction(Transaction transaction)
at System.Transactions.TransactionInterop.GetExportCookie(Transaction transaction, Byte[] whereabouts)
at Microsoft.Data.SqlClient.SqlInternalConnection.GetTransactionCookie(Transaction transaction, Byte[] whereAbouts)
at Microsoft.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
at Microsoft.Data.ProviderBase.DbConnectionPool.PrepareConnection(DbConnection owningObject, DbConnectionInternal obj, Transaction transaction)
at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInt
Include provider and version information
EF Core version: 8.0.2 Database provider: MSSQL, Oracle Target framework: DOTNET CORE 8 Operating system: WINDOWS 10 IDE: Visual Studio 2022
This looks like an issue in System.Transactions rather than in EF.
Can you confirm whether this happens consistently, or only occasionally? Are you able to reproduce the same thing with two SQL Server connections rather than Oracle and SQL Server?
It happens consistently. I did not try with two SQL server connection, but tried with just one.. And it worked fine. I will try out two different connections and will post how it goes here
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.