Distributed Transactions: .NET Framework vs .NET Core
Asked Answered
G

2

10

I have the following code sample:

static void Main(string[] args)
{
    TransactionManager.DistributedTransactionStarted += (sender, eventArgs) =>
    {
        Console.WriteLine("Promoted to distributed transaction!");
    };
​
    const string connectionString = @"Server=localhost\SQLEXPRESS;Database=master;Integrated Security=true;";
​
    using (var tx = new TransactionScope())
    using (var conn1 = new SqlConnection(connectionString))
    using (var conn2 = new SqlConnection(connectionString))
    {
        conn1.Open();
        Console.WriteLine("conn1 opened");
​
        conn2.Open();
        Console.WriteLine("conn2 opened");
​
        tx.Complete();
    }
​
    Console.ReadLine();
​
}

When executing this code in a .NET Framework (4.8) Console Application (against SQL Server Express 2017) it yields the following output:

Output of code in .NET Framework Console

Since the transaction is being promoted to a Distributed Transaction, I expect a similar Console Application targeting .NET Core (3.0) to throw a

System.PlatformNotSupportedException (This platform does not support distributed transactions.).

However, the actual output is:

Output of code in .NET Core Console

Why is this? I expect the promotion of a transaction to a distributed transaction to be framework-agnostic.


Edit: This .NET Core (3.0) code sample does something with the database connections:

Database Schema:

CREATE DATABASE [TestDB1] 
GO
CREATE TABLE [TestDB1].[dbo].[Table]([Value] [nvarchar](max) NULL) 

.NET Core (3.0) Console Application:

static void Main(string[] args)
{
    TransactionManager.DistributedTransactionStarted += (sender, eventArgs) =>
    {
        Console.WriteLine("Promoted to distributed transaction!");
    };

    const string connectionString = @"Server=localhost\SQLEXPRESS;Database=TestDB1;Integrated Security=true;";

    using (var tx = new TransactionScope())
    using (var conn1 = new SqlConnection(connectionString))
    using (var conn2 = new SqlConnection(connectionString))
    {
        conn1.Open();
        Console.WriteLine("conn1 opened");
        using (var cmd1 = conn1.CreateCommand())
        {
            cmd1.CommandText = "INSERT INTO [dbo].[Table] ([Value]) VALUES ('test 1')";
            cmd1.ExecuteNonQuery();
            Console.WriteLine("Record inserted through conn1");
        }

        conn2.Open();
        Console.WriteLine("conn2 opened");
        using (var cmd2 = conn2.CreateCommand())
        {
            cmd2.CommandText = "INSERT INTO [dbo].[Table] ([Value]) VALUES ('test 1')";
            cmd2.ExecuteNonQuery();
            Console.WriteLine("Record inserted through conn2");
        }

        tx.Complete();
        Console.WriteLine("Transaction completed");
    }

    Console.ReadLine();
}

And the Console output:

.NET Core Console output when doing something with the connections

Note: This sample also succeeds when using 2 different connectionstrings for the 2 connections!

Garek answered 2/12, 2019 at 8:56 Comment(3)
Try to actually do something from the connections.Bolection
I cannot reproduce this -- not on .NET Core 3, not on 3.1, not with System.Data.SqlClient, not with Microsoft.Data.SqlClient. All variations produce an exception.Errolerroll
Does transactionscope introduced in .Net Core 2.0 cover your use-case? Have a look at its example.Vender
M
7

NET Core doesn't support Distributed Transactions because it would require a different transaction manager on each platform. You can find more about this issue on https://github.com/dotnet/runtime/issues/715.

This feature seems to be added to Release 5.0 from .NET Due by November 30, 2020

There is another thread SA talking about this problem.

Mesenchyme answered 22/3, 2020 at 14:54 Comment(1)
OK, that makes sense. So why does @Garek fail to get an exception?Aqualung
M
2

Distributed Transactions support was merged 3 hours ago for modern .NET https://github.com/dotnet/runtime/pull/72051

Mucosa answered 12/8, 2022 at 13:10 Comment(2)
Does this mean that distributed transaction work in .net core. Because as far as I can see for .net core 7 not .net core 5. Has anyon tested this?Mosa
The support is Windows-only, and is a largely a straight port of C++ code to C#.Weariful

© 2022 - 2024 — McMap. All rights reserved.