Cross-instance distributed transactions with Azure SQL Managed Instance
Published Apr 29 2021 01:33 PM 6,227 Views
Microsoft

Edit (August 2022): Private preview for MS DTC support for Azure SQL Managed Instance is available.

Introduction

With Azure SQL Managed Instance, you don't need MS DTC to run distributed transactions! You can now migrate to Azure or build distributed applications with Managed Instance which natively supports cross-instance T-SQL and .NET distributed transactions

 

With recent modernization, distributed transactions are supported between Managed Instances as the only supported transaction participants. If your application runs .NET or T-SQL distributed transactions on SQL Server and of course requires MS DTC for that, you can now migrate to Azure SQL Managed Instance and distributed transactions will simply work. Also, if you need to build a multi-server environment in the cloud that needs to run queries in a transactionally consistent fashion, Managed Instance with native support for distributed transactions is a great place for your app.

 

Distributed transactions on Managed Instance can easily be setup with two simple steps:

  1. Create Server trust group.
  2. Ensure there is network connectivity between your instances.

In the rest of the blog, you will get more info on how to easily setup your Managed Instance to run distributed transactions.

 

Creating a Server trust group

Server trust group (STG) is an entity that manages security requirements for running distributed transactions. You can create it in Azure Portal, or via Azure PowerShell  or Azure CLI.

 

Here is an example of Azure PowerShell script that creates STG for two Managed Instances.

Login-AzAccount
Select-AzSubscription -SubscriptionId "c829c89a-0931-4310-a49c-fa8f42a8cff2"
$managedInstanceList = @()
$managedInstanceList += Get-AzSqlInstance -Name "sqlmi1" -ResourceGroupName "rg1"
$managedInstanceList += Get-AzSqlInstance -Name "sqlmi2" -ResourceGroupName "rg1"
New-AzSqlServerTrustGroup -ResourceGroupName "rg1" -Location "West Europe" -Name "stg1" -GroupMember $managedInstanceList -TrustScope "GlobalTransactions"

Network connectivity

For distributed transactions to work, there must be a network connectivity between Managed Instances. If instances are on different virtual networks, VNET peering needs to be setup. 
Additionally, port 5024 and port range 11000-12000 need to be allowed for outbound and inbound VNET traffic in the Network Security Groups assigned to Subnets that host Managed Instances.

 

Note: when Server trust group is not created, or there is no network connectivity between instances, distributed transaction will fail with following error message.

Msg 8510, Level 20, State 3, Line 14
Enlist operation failed: Global Transaction failed. SQL Server could not register with Microsoft Distributed Transaction Coordinator (MS DTC) as a resource manager for this transaction. The transaction may have been stopped by the client or the resource manager.

Distributed Transaction examples

With STG and network settings in place, you can run distributed transactions across Managed Instances with linked servers.

Here's an example with two SELECT queries against master database.

SET XACT_ABORT ON
BEGIN DISTRIBUTED TRANSACTION
	-- select from local server
	SELECT * FROM master.sys.databases
	-- select from remote, linked server
	SELECT * FROM [linked_server_02].master.sys.databases
COMMIT

Here is another example with INSERT queries against user databases.

SET XACT_ABORT ON
BEGIN DISTRIBUTED TRANSACTION
	-- insert into local server
	INSERT INTO db01.dbo.t01 (tag, utc_time)
		VALUES ('distributed_transaction_tag', GETUTCDATE())
	-- insert into remote, linked server
	INSERT INTO [linked_server_02].db01.dbo.t01 (tag, utc_time)
		VALUES ('distributed_transaction_tag', GETUTCDATE())
COMMIT

Additional examples with .NET TransactionScope are available in the documentation.

7 Comments
Copper Contributor

If it works like the "Cross-database for MI" good luck with that... Still in "preview" (which means buggy) since 2019.
Anyway, I will give it a try and I will comment here. 

Microsoft

Hi @fmantovani, I'm Sasa Popovic, program manager working on Managed Instance distributed transactions. Thank you for the feedback and interest in the feature! If you have more feedback please feel free to post in comments or to contact me via IM/email.
I'd like to address few points from your comment. Managed Instance distributed transactions are in public preview since October 2020. We consider public preview to be a public beta, meaning that anyone with Azure subscription can try the feature and Microsoft Customer Support Services will supply support services. Features in public preview are more likely to have limited functionality (e.g. you can put only two Managed Instances in Server trust group) than to be buggy, and I can tell that at this point we don't have any known outstanding bugs related to Managed Instance distributed transactions.

Copper Contributor

Hi @sasapopo 
Today we have multiple WCF-services which use MSDTC in a On-Prem solution. We only have transactions across the databases NOT across the WCF-services. Our WCF-services are in .NET 4.1. We are planning to move all our applications and databases to Azure.

My question is:
Do we need to update our WCF-services to .NET 4.6.1?

Copper Contributor

Hi @sasapopo 
At least I got answer from our developer.

 

We are using TransationScope and our WCF-services are in .NET 4.5/4.5.2.

I think we don’t need to update our WCF-services to .NET 4.6.1 because Sasa Popovic said “If they are using .net and transationscope class, their system should work on MI”.

Microsoft

@lagorf01 according to the official .NET documentation, TransactionScope is supported in .NET Framework 4.5: https://docs.microsoft.com/en-us/dotnet/api/system.transactions.transactionscope?view=netframework-4...

Based on that I would say that there is not need to upgrade to 4.6.1.

Copper Contributor

got this error:

Msg 21, Level 20, State 1, Line 11
Warning: Fatal error 8510 occurred at Oct 6 2021 3:08PM. Note the error and time, and contact your system administrator.

when running:

SET XACT_ABORT ON
BEGIN DISTRIBUTED TRANSACTION

select * from [sqlmi-linked-server].tpcc1000.dbo.new_order where no_o_id = 2847 and no_d_id = 8 order by no_w_id asc

COMMIT

 

 

Microsoft

Hi @glin88 , if you are still experiencing the same error when trying to execute distributed transaction with your Azure SQL Managed Instance, please file a support ticket to engage with our support engineers.

Co-Authors
Version history
Last update:
‎Aug 03 2022 07:50 AM
Updated by: