Distributed transactions available now for Azure SQL Managed Instance

Published Nov 02 2021 07:40 AM 1,537 Views
Microsoft

Introduction

Azure SQL Managed Instance distributed transactions are generally available! You can now run .NET and T-SQL distributed transactions across multiple Managed Instances. We support distributed transactions across Managed Instances in different regions, subscriptions and VNETs.

T-SQL and .NET distributed transactions across multiple Managed InstancesT-SQL and .NET distributed transactions across multiple Managed Instances

Preparation

To setup this functionality you need put all Managed Instances into Server trust group. This can be done in Azure Portal or via Azure PowerShell or CLI.

With Azure PowerShell you can create new Server trust group with New-AzSqlServerTrustGroup. Here is an example of that.

 

$managedInstanceList = @()

# Select instances from the first subscription.
Select-AzSubscription 54afce91-1f05-4c0a-bb97-8954574a94e0
$mi = Get-AzSqlInstance -ResourceId "/subscriptions/54afce91-1f05-4c0a-bb97-8954574a94e0/resourceGroups/rg1/providers/Microsoft.Sql/managedInstances/sqlmi01"
$managedInstanceList += $mi
$mi = Get-AzSqlInstance -ResourceId "/subscriptions/54afce91-1f05-4c0a-bb97-8954574a94e0/resourceGroups/rg1/providers/Microsoft.Sql/managedInstances/sqlmi02"
$managedInstanceList += $mi

# Select instances from the second subscription.
Select-AzSubscription 16d503da-a739-4ac9-9b3d-bfd82bec441f
$mi = Get-AzSqlInstance -ResourceId "/subscriptions/16d503da-a739-4ac9-9b3d-bfd82bec441f/resourceGroups/rg2/providers/Microsoft.Sql/managedInstances/sqlmi10"
$managedInstanceList += $mi

# Create new trust group.
Select-AzSubscription 54afce91-1f05-4c0a-bb97-8954574a94e0
New-AzSqlServerTrustGroup -ResourceGroupName "rg1" -Location "West Europe" -Name "TrustGroup1" -GroupMember $managedInstanceList -TrustScope "GlobalTransactions, ServiceBroker" 

 

Or you can edit you existing Server trust group with Set-AzSqlServerTrustGroup, as shown in the example below.

 

$managedInstanceList = @()

# Select instances from the first subscription.
Select-AzSubscription 54afce91-1f05-4c0a-bb97-8954574a94e0
$mi = Get-AzSqlInstance -ResourceId "/subscriptions/54afce91-1f05-4c0a-bb97-8954574a94e0/resourceGroups/rg1/providers/Microsoft.Sql/managedInstances/sqlmi01"
$managedInstanceList += $mi
$mi = Get-AzSqlInstance -ResourceId "/subscriptions/54afce91-1f05-4c0a-bb97-8954574a94e0/resourceGroups/rg1/providers/Microsoft.Sql/managedInstances/sqlmi02"
$managedInstanceList += $mi
$mi = Get-AzSqlInstance -ResourceId "/subscriptions/54afce91-1f05-4c0a-bb97-8954574a94e0/resourceGroups/rg1/providers/Microsoft.Sql/managedInstances/sqlmi03"
$managedInstanceList += $mi

# Select instances from the second subscription.
Select-AzSubscription 16d503da-a739-4ac9-9b3d-bfd82bec441f
$mi = Get-AzSqlInstance -ResourceId "/subscriptions/16d503da-a739-4ac9-9b3d-bfd82bec441f/resourceGroups/rg2/providers/Microsoft.Sql/managedInstances/sqlmi10"
$managedInstanceList += $mi

# Set the trust group.
Select-AzSubscription 54afce91-1f05-4c0a-bb97-8954574a94e0
Set-AzSqlServerTrustGroup -ResourceGroupName "rg1" -Location "West Europe" -Name "TrustGroup1" -GroupMember $managedInstanceList -TrustScope "GlobalTransactions, ServiceBroker"

 

If instances are in different VNETs then of course VNET peering needs to setup. On all VNETs, ports 5024, and 11000-12000 need to be allowed.

Usage examples for T-SQL and .NET

Once the preparations are over you can run distributed transactions from T-SQL, like in the example below. We are using linked servers to access all remote instances.

 

SET XACT_ABORT ON
BEGIN DISTRIBUTED TRANSACTION
    INSERT INTO db01.dbo.t01 (tag, utc_time) VALUES (@app_tag, GETUTCDATE())
    INSERT INTO [sqlmi02].db01.dbo.t01 (tag, utc_time) VALUES (@app_tag, GETUTCDATE())
    INSERT INTO [sqlmi03].db01.dbo.t01 (tag, utc_time) VALUES (@app_tag, GETUTCDATE())
    INSERT INTO [sqlmi10].db01.dbo.t01 (tag, utc_time) VALUES (@app_tag, GETUTCDATE())
COMMIT

 

Also, you can run distributed transactions from .NET, like shown in the example below.

 

using (var scope = new TransactionScope())
{
    // Execute transaction on all 10 connections.
    //
    for (int i = 0; i < 10; i++)
    {
        using (var conn = new SqlConnection(sqlManagedInstanceConnStr[i]))
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = string.Format("insert into T1 values(1)");
            cmd.ExecuteNonQuery();
        }
    }
    
    // Complete distributed transaction on all 10 Managed Instances.
    //
    scope.Complete();
}

 

 

Next steps

We hope that this new functionality will enable you to write new modern distributed applications or easily migrate your existing applications to Managed Instance.

 

If you have questions, feedback or additional feature requests related to distributed transactions, let me know in the comments! Happy distributed transactions everyone!

%3CLINGO-SUB%20id%3D%22lingo-sub-2912978%22%20slang%3D%22en-US%22%3EDistributed%20transactions%20available%20now%20for%20Azure%20SQL%20Managed%20Instance%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2912978%22%20slang%3D%22en-US%22%3E%3CH2%20id%3D%22toc-hId--298937518%22%20id%3D%22toc-hId--298882740%22%3EIntroduction%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-sql%2Fdatabase%2Felastic-transactions-overview%23transactions-across-multiple-servers-for-azure-sql-managed-instance%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20SQL%20Managed%20Instance%20distributed%20transactions%3C%2FA%3E%20are%20generally%20available!%20You%20can%20now%20run%20.NET%20and%20T-SQL%20distributed%20transactions%20across%20%3CSTRONG%3Emultiple%20Managed%20Instances%3C%2FSTRONG%3E.%20We%20support%20distributed%20transactions%20across%20Managed%20Instances%20in%20%3CSTRONG%3Edifferent%20regions%2C%20subscriptions%20and%20VNETs%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Diagram.png%22%20style%3D%22width%3A%20778px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F323109iD701E6ACA9A58A49%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Diagram.png%22%20alt%3D%22T-SQL%20and%20.NET%20distributed%20transactions%20across%20multiple%20Managed%20Instances%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ET-SQL%20and%20.NET%20distributed%20transactions%20across%20multiple%20Managed%20Instances%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--2106391981%22%20id%3D%22toc-hId--2106337203%22%3EPreparation%3C%2FH2%3E%0A%3CP%3ETo%20setup%20this%20functionality%20you%20need%20put%20all%20Managed%20Instances%20into%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-sql%2Fmanaged-instance%2Fserver-trust-group-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EServer%20trust%20group%3C%2FA%3E.%20This%20can%20be%20done%20in%20Azure%20Portal%20or%20via%20Azure%20PowerShell%20or%20CLI.%3C%2FP%3E%0A%3CP%3EWith%20Azure%20PowerShell%20you%20can%20create%20new%20Server%20trust%20group%20with%20New-AzSqlServerTrustGroup.%20Here%20is%20an%20example%20of%20that.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24managedInstanceList%20%3D%20%40()%0A%0A%23%20Select%20instances%20from%20the%20first%20subscription.%0ASelect-AzSubscription%2054afce91-1f05-4c0a-bb97-8954574a94e0%0A%24mi%20%3D%20Get-AzSqlInstance%20-ResourceId%20%22%2Fsubscriptions%2F54afce91-1f05-4c0a-bb97-8954574a94e0%2FresourceGroups%2Frg1%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2Fsqlmi01%22%0A%24managedInstanceList%20%2B%3D%20%24mi%0A%24mi%20%3D%20Get-AzSqlInstance%20-ResourceId%20%22%2Fsubscriptions%2F54afce91-1f05-4c0a-bb97-8954574a94e0%2FresourceGroups%2Frg1%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2Fsqlmi02%22%0A%24managedInstanceList%20%2B%3D%20%24mi%0A%0A%23%20Select%20instances%20from%20the%20second%20subscription.%0ASelect-AzSubscription%2016d503da-a739-4ac9-9b3d-bfd82bec441f%0A%24mi%20%3D%20Get-AzSqlInstance%20-ResourceId%20%22%2Fsubscriptions%2F16d503da-a739-4ac9-9b3d-bfd82bec441f%2FresourceGroups%2Frg2%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2Fsqlmi10%22%0A%24managedInstanceList%20%2B%3D%20%24mi%0A%0A%23%20Create%20new%20trust%20group.%0ASelect-AzSubscription%2054afce91-1f05-4c0a-bb97-8954574a94e0%0ANew-AzSqlServerTrustGroup%20-ResourceGroupName%20%22rg1%22%20-Location%20%22West%20Europe%22%20-Name%20%22TrustGroup1%22%20-GroupMember%20%24managedInstanceList%20-TrustScope%20%22GlobalTransactions%2C%20ServiceBroker%22%20%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOr%20you%20can%20edit%20you%20existing%20Server%20trust%20group%20with%20Set-AzSqlServerTrustGroup%2C%20as%20shown%20in%20the%20example%20below.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24managedInstanceList%20%3D%20%40()%0A%0A%23%20Select%20instances%20from%20the%20first%20subscription.%0ASelect-AzSubscription%2054afce91-1f05-4c0a-bb97-8954574a94e0%0A%24mi%20%3D%20Get-AzSqlInstance%20-ResourceId%20%22%2Fsubscriptions%2F54afce91-1f05-4c0a-bb97-8954574a94e0%2FresourceGroups%2Frg1%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2Fsqlmi01%22%0A%24managedInstanceList%20%2B%3D%20%24mi%0A%24mi%20%3D%20Get-AzSqlInstance%20-ResourceId%20%22%2Fsubscriptions%2F54afce91-1f05-4c0a-bb97-8954574a94e0%2FresourceGroups%2Frg1%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2Fsqlmi02%22%0A%24managedInstanceList%20%2B%3D%20%24mi%0A%24mi%20%3D%20Get-AzSqlInstance%20-ResourceId%20%22%2Fsubscriptions%2F54afce91-1f05-4c0a-bb97-8954574a94e0%2FresourceGroups%2Frg1%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2Fsqlmi03%22%0A%24managedInstanceList%20%2B%3D%20%24mi%0A%0A%23%20Select%20instances%20from%20the%20second%20subscription.%0ASelect-AzSubscription%2016d503da-a739-4ac9-9b3d-bfd82bec441f%0A%24mi%20%3D%20Get-AzSqlInstance%20-ResourceId%20%22%2Fsubscriptions%2F16d503da-a739-4ac9-9b3d-bfd82bec441f%2FresourceGroups%2Frg2%2Fproviders%2FMicrosoft.Sql%2FmanagedInstances%2Fsqlmi10%22%0A%24managedInstanceList%20%2B%3D%20%24mi%0A%0A%23%20Set%20the%20trust%20group.%0ASelect-AzSubscription%2054afce91-1f05-4c0a-bb97-8954574a94e0%0ASet-AzSqlServerTrustGroup%20-ResourceGroupName%20%22rg1%22%20-Location%20%22West%20Europe%22%20-Name%20%22TrustGroup1%22%20-GroupMember%20%24managedInstanceList%20-TrustScope%20%22GlobalTransactions%2C%20ServiceBroker%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20instances%20are%20in%20different%20VNETs%20then%20of%20course%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fvirtual-network%2Ftutorial-connect-virtual-networks-portal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EVNET%20peering%20needs%20to%20setup%3C%2FA%3E.%20On%20all%20VNETs%2C%20ports%205024%2C%20and%2011000-12000%20need%20to%20be%20allowed.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-381120852%22%20id%3D%22toc-hId-381175630%22%3EUsage%20examples%20for%20T-SQL%20and%20.NET%3C%2FH2%3E%0A%3CP%3EOnce%20the%20preparations%20are%20over%20you%20can%20run%20distributed%20transactions%20from%20T-SQL%2C%20like%20in%20the%20example%20below.%20We%20are%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Flinked-servers%2Flinked-servers-database-engine%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Elinked%20servers%3C%2FA%3E%20to%20access%20all%20remote%20instances.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESET%20XACT_ABORT%20ON%0ABEGIN%20DISTRIBUTED%20TRANSACTION%0A%20%20%20%20INSERT%20INTO%20db01.dbo.t01%20(tag%2C%20utc_time)%20VALUES%20(%40app_tag%2C%20GETUTCDATE())%0A%20%20%20%20INSERT%20INTO%20%5Bsqlmi02%5D.db01.dbo.t01%20(tag%2C%20utc_time)%20VALUES%20(%40app_tag%2C%20GETUTCDATE())%0A%20%20%20%20INSERT%20INTO%20%5Bsqlmi03%5D.db01.dbo.t01%20(tag%2C%20utc_time)%20VALUES%20(%40app_tag%2C%20GETUTCDATE())%0A%20%20%20%20INSERT%20INTO%20%5Bsqlmi10%5D.db01.dbo.t01%20(tag%2C%20utc_time)%20VALUES%20(%40app_tag%2C%20GETUTCDATE())%0ACOMMIT%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlso%2C%20you%20can%20run%20distributed%20transactions%20from%20.NET%2C%20like%20shown%20in%20the%20example%20below.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20(var%20scope%20%3D%20new%20TransactionScope())%0A%7B%0A%20%20%20%20%2F%2F%20Execute%20transaction%20on%20all%2010%20connections.%0A%20%20%20%20%2F%2F%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%26lt%3B%2010%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20using%20(var%20conn%20%3D%20new%20SqlConnection(sqlManagedInstanceConnStr%5Bi%5D))%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20conn.Open()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20SqlCommand%20cmd%20%3D%20conn.CreateCommand()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20cmd.CommandText%20%3D%20string.Format(%22insert%20into%20T1%20values(1)%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20cmd.ExecuteNonQuery()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20Complete%20distributed%20transaction%20on%20all%2010%20Managed%20Instances.%0A%20%20%20%20%2F%2F%0A%20%20%20%20scope.Complete()%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1426333611%22%20id%3D%22toc-hId--1426278833%22%3ENext%20steps%3C%2FH2%3E%0A%3CP%3EWe%20hope%20that%20this%20new%20functionality%20will%20enable%20you%20to%20write%20new%20modern%20distributed%20applications%20or%20easily%20migrate%20your%20existing%20applications%20to%20Managed%20Instance.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20have%20questions%2C%20feedback%20or%20additional%20feature%20requests%20related%20to%20distributed%20transactions%2C%20let%20me%20know%20in%20the%20comments!%20Happy%20distributed%20transactions%20everyone!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2912978%22%20slang%3D%22en-US%22%3E%3CP%3EWe%E2%80%99re%20happy%20to%20let%20you%20know%20that%20distributed%20transactions%20for%20Managed%20Instance%20are%20GA!%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22distributed%20transaction%20teaser.png%22%20style%3D%22width%3A%20498px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F323030i7C77BCDB4D9D8B8E%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22distributed%20transaction%20teaser.png%22%20alt%3D%22Distributed%20transactions%20on%20Azure%20SQL%20Managed%20Instance%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EDistributed%20transactions%20on%20Azure%20SQL%20Managed%20Instance%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2912978%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20SQL%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20SQL%20Managed%20Instance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Ignite%202021%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Nov 02 2021 09:17 AM
Updated by: