SQL2016 AG開始支援MSDTC,MSDTC幫我們保證分散式交易的一致性,
交易期間如果某一SERVER發生問題,整包交易相關資料全部rollback,
這功能我相信對.NET應該不陌生(我個人幾乎很常使用)。
SQL2016分散式交易只支援交易跨多台SQL Server Instance,不支援交易在同一台SQL Server Instance跨多個資料庫。
開始設定AG支援MSDTC
建立AG時須搭配DTC_SUPPORT =XXX,目前並無法透過alter AG來修改(這點到是有點不太方便)。
我先透過AG wizard產生create scripts,並修改DTC_SUPPORT=XX
CREATE AVAILABILITY GROUP [AG1]
WITH (AUTOMATED_BACKUP_PREFERENCE = NONE,
DB_FAILOVER = ON,
DTC_SUPPORT = PER_DB)
FOR DATABASE [DemoQueryStore]
REPLICA ON N'WIN2K12R2A\SQL2K16' WITH (ENDPOINT_URL = N'TCP://win2k12R2A.RS.com.tw:5022', FAILOVER_MODE = AUTOMATIC, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 50, SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL)),
N'WIN2K12R2B\SQL2K16' WITH (ENDPOINT_URL = N'TCP://win2k12R2B.RS.com.tw:5022', FAILOVER_MODE = AUTOMATIC, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 50, SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL));
確認AG的MSDTC
select name,dtc_support
from sys.availability_groups
每一個node都啟動MSDTC服務(記得防火牆允許分散式協調器)
啟動後可以再Log看到MSDTC註冊成功訊息
LinkedServer設定如下
AG中測試測試分散式交易
begin distributed tran
set xact_abort on
-- select * from sqlb.testdtS.dbo.[testA]
insert into dbo.testA select 1,1,null
insert into sqlb.testdtS.dbo.[testA] select 1,1,null
commit
看到交易統計資料
參考
CREATE AVAILABILITY GROUP (Transact-SQL)
SQL Server 2016 DTC Support In Availability Groups
MSDTC Recommendations on SQL Failover Cluster
Transactions - Always On availability groups and Database Mirroring
How to Properly Configure DTC for Clustered Instances of SQL Server (Revised)