Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null reference when calling SaveChangesAsync method using transaction scope - Distributed transaction - Oracle and MSSQL #33662

Open
hafizca opened this issue May 2, 2024 · 2 comments

Comments

@hafizca
Copy link

hafizca commented May 2, 2024

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

@roji
Copy link
Member

roji commented May 2, 2024

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?

@hafizca
Copy link
Author

hafizca commented May 2, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants