Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Distributed transactions are essential for ensuring data consistency across multiple databases or systems. In a Windows environment, the Microsoft Distributed Transaction Coordinator (MSDTC) is a service that facilitates distributed transactions. This article will guide you through setting up and managing distributed transactions using MSDTC on Windows.
A distributed transaction involves multiple networked databases or systems. It ensures that all participating databases commit or roll back changes in a coordinated manner, maintaining data integrity.
Open Server Manager:
Win + R
, type ServerManager
, and press Enter.Add the Distributed Transaction Coordinator feature:
Manage
> Add Roles and Features
.Features
section.Distributed Transaction Coordinator
.Configure MSDTC:
Component Services
by pressing Win + R
, typing dcomcnfg
, and pressing Enter.Component Services
> Computers
> My Computer
> Distributed Transaction Coordinator
.Local DTC
and select Properties
.Network DTC Access
is enabled.Open Windows Firewall:
Win + R
, type wf.msc
, and press Enter.Create Inbound Rules:
Inbound Rules
.MSDTC.exe
(typically located in C:\Windows\System32\msdtc.exe
).Create Outbound Rules:
MSDTC.exe
.Here's a practical example of how to use MSDTC in a .NET application to manage a distributed transaction involving two SQL Server databases.
using System;
using System.Data.SqlClient;
using System.Transactions;
class Program
{
static void Main()
{
string connectionString1 = "Data Source=Server1;Initial Catalog=Database1;Integrated Security=True;";
string connectionString2 = "Data Source=Server2;Initial Catalog=Database2;Integrated Security=True;";
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection connection1 = new SqlConnection(connectionString1))
{
connection1.Open();
SqlCommand command1 = new SqlCommand("INSERT INTO Table1 (Column1) VALUES ('Value1')", connection1);
command1.ExecuteNonQuery();
}
using (SqlConnection connection2 = new SqlConnection(connectionString2))
{
connection2.Open();
SqlCommand command2 = new SqlCommand("INSERT INTO Table2 (Column2) VALUES ('Value2')", connection2);
command2.ExecuteNonQuery();
}
scope.Complete();
}
Console.WriteLine("Transaction completed successfully.");
}
}
Check MSDTC Logs:
Event Viewer
and navigate to Applications and Services Logs
> Microsoft
> Distributed Transaction Coordinator
.Verify Network Communication:
ping
and telnet
commands to test connectivity.