跨 Azure SQL 和本地 sql 服务器的分布式事务导致错误

Jus*_*ard 4 sql-server msdtc transactionscope azure-sql-database

是否可以在本地 SQL Server 实例和 Azure SQL 数据库之间进行事务。

我有以下测试用例。

public class TransactionsTest
{
    [Fact]
    public void Test1()
    {
        var premisesDatabaseContext = new OnPremisesDatabaseContext();
        var azureSQLDatabaseContext = new AzureSQLDatabaseContext();

        using (TransactionScope scope = new TransactionScope())
        {
            premisesDatabaseContext.Database.Connection.Open();
            azureSQLDatabaseContext.Database.Connection.Open();

            scope.Complete();
        }
    }

    [Fact]
    public void Test2()
    {
        var premisesDatabaseContext = new OnPremisesDatabaseContext();
        var azureSQLDatabaseContext = new AzureSQLDatabaseContext();

        using (TransactionScope scope = new TransactionScope())
        {
            azureSQLDatabaseContext.Database.Connection.Open();
            premisesDatabaseContext.Database.Connection.Open();

            scope.Complete();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这看起来很简单。但是当我打开第二个连接时,两个测试用例都失败并出现不同的错误。

这是错误的详细信息。

// TEST 1
System.Reflection.TargetInvocationException: 
Exception has been thrown by the target of an invocation. 
--->
System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Transactions.Transaction.GetPromotedToken()

// TEST 2
System.Transactions.TransactionPromotionException:
There is a promotable enlistment for the transaction which has a PromoterType value that is not recognized by System.Transactions. 
1c742caf-6680-40ea-9c26-6b6846079764
Run Code Online (Sandbox Code Playgroud)

我想知道是否有可能实现这一点。如果不可能,有什么替代方案?

Jus*_*ard 9

你不能

Azure sql 数据库使用弹性事务。虽然本地服务器使用Microsoft Distributed Transaction Coordinator (MSDTC).

azure 不支持 MSDTC,根据弹性交易文档,

仅支持 SQL DB 中跨数据库的事务。SQL DB 之外的其他 X/Open XA 资源提供程序和数据库无法参与弹性数据库事务。这意味着弹性数据库事务不能跨越本地 SQL Server 和 Azure SQL 数据库。对于本地分布式事务,请继续使用 MSDTC。

  • 本地 SQL Server 使用 MSDTC,这在 Azure SQL 数据库中不可用。

  • Azure SQL 数据库使用弹性事务,它不适用于本地 SQL 服务器。

最简单的解决方案是将所有数据库移动到 Azure 或本地。