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

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

问题描述

我有以下代码示例:

static void Main(string[] args)
{
    TransactionManager.DistributedTransactionStarted += (sender, eventArgs) =>
    {
        Console.WriteLine("Promoted to distributed transaction!");
    };
​
    const string connectionString = @"Server=localhostSQLEXPRESS;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(此平台不支持分布式事务.).

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

然而,实际输出是:

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

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=localhostSQLEXPRESS;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 个连接使用 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.

此功能似乎已添加到 .NET 的 5.0 版中,截止日期为 2020 年 11 月 30 日

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天全站免登陆