I have a deadlock on a distributed transaction in a .NET 8 environment, but I don't understand how to resolve it

137 Views Asked by At

I have created a console application using the .Net 8 environment and there is this code in Program.cs:

TransactionManager.ImplicitDistributedTransactions = true;
var transactionOptions = new TransactionOptions();
transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
transactionOptions.Timeout = TimeSpan.FromSeconds(3600);


using (IDbConnection connection1 = new SqlConnection(infocitiConnectionString))
using (IDbConnection connection2 = new SqlConnection(storageConnectionString))
{
    TransactionScope ts = null;
    try
    {
        ts = new TransactionScope(TransactionScopeOption.Required, transactionOptions);
        {

            connection1.Open();
            //connection1.Execute(sql: sqlDelete);
            connection1.Close();

            connection2.Open();
            //connection2.Execute(sql: sqlInsert);
            connection2.Close();

            ts.Complete();
        }
    }
    finally
    {
        ts?.Dispose();
    }
}

Unfortunately, I get a deadlock on the code line:

ts?.Dispose();

Deadlock Schema

I searched the internet and the net 8 source code but found nothing clear

If I comment out the ts.Complete(); code, the deadlock disappears.

1

There are 1 best solutions below

0
Maksim Perevoshchikov On

I updated the Microsoft.Data.SqlClient package from version 2.1.5 to last actual version(5.1.2) and the deadlock went away