分布式事务:.NET Framework与.NET Core [英] Distributed Transactions: .NET Framework vs .NET Core

查看:90
本文介绍了分布式事务:.NET Framework与.NET Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码示例:

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();
​
}

在.NET Framework(4.8)控制台应用程序(针对SQL Server Express 2017)中执行此代码时,将产生以下输出:

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

由于将事务提升为分布式事务,因此我期望针对.NET Core(3.0)的类似控制台应用程序会抛出

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(该平台不支持分布式事务.).

但是,实际输出是:

这是为什么?我希望将事务升级为分布式事务与框架无关.

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

此.NET Core(3.0)代码示例对数据库连接有作用:

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

数据库架构:

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

.NET Core(3.0)控制台应用程序:

.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();
}

控制台输出:

注意:当两个连接使用2个不同连接字符串时,此示例也会成功!

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

推荐答案

NET Core 不支持分布式事务,因为在每个平台上都需要不同的事务管理器.您可以在 https://github.com/dotnet/runtime/issues/上找到有关此问题的更多信息715 .

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.

此功能似乎已在2020年11月30日之前从.NET Due版本5.0中添加

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

还有另一个线程 SA 谈论这个问题.

There is another thread SA talking about this problem.

这篇关于分布式事务:.NET Framework与.NET Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆