TransactionScope 之分布式配置

TransactionScope是个好东西,可以自动管理transaction,即使是对分布式数据库也可以,但是需要一些配置(摸索了2天。。。), 本文的环境为windows 2003:

 

1. 对跑代码的机器sql所在的服务器进行Component Services的配置

Administative Tools -> Component Services -> 点开Component Services -> Computers -> My computer -> 右键属性 -> 选择MSDTC -> Security Configuration -> 按如下配置 -> OK -> 重启Distributed Transaction Coordinator服务(一定要手动重启哟

最重要的是Allow Inbound 和 Allow Outbound, 其他意义如下:

 

Setting

Description

Corresponding registry value

Network DTC Access

Determines whether DTC on the local computer is allowed to access the network. This setting must be enabled in combination with one of the other settings to enable network DTC transactions.

Default setting: Off

Security/NetworkDtcAccess

0 = Off

1 = On

Allow inbound

Allows a distributed transaction that originates from a remote computer to run on this computer.

Default setting: Off

To enable this setting you must set the following registry key values to 1:

Security/NetworkDtcAccess

Security/NetworkDtcAccessTransactions

Security/NetworkDtcAccessInbound

To disable this setting, you only need to set the following registry key value to 0:

Security/NetworkDtcAccessInbound

Allow Outbound

Allows the local computer to initiate a transaction and run it on a remote computer.

To enable this setting, you need to set the following registry key values to 1:

Security/NetworkDtcAccess

Security/ NetworkDtcAccessTransactions

Security/ NetworkDtcAccessOutbound

To disable this setting, you only need to set the following registry key value to 0:

Security/NetworkDtcAccessOutbound

Mutual Authentication Required

Adds support for mutual authentication in future versions and is the highest secured communication mode. This is the recommended transaction mode for clients running Windows XP SP2 and servers running one of the Windows Server 2003 operating systems.

AllowOnlySecureRpcCalls = 1

FallbackToUnsecureRPCIfNecessary = 0

TurnOffRpcSecurity = 0

Incoming Caller Authentication Required

Requires the local DTC to communicate with a remote DTC using only encrypted messages and mutual authentication. This setting is recommended for servers running Windows Server 2003 that are operating in a cluster.

Only Windows Server 2003 and Windows XP SP2 support this feature, so you should only use this if you know that the DTC on the remote computer runs either the Windows Server 2003 or Windows XP SP2 operating system.

AllowOnlySecureRpcCalls = 0

FallbackToUnsecureRPCIfNecessary = 1

TurnOffRpcSecurity = 0

No Authentication Required

Provides system compatibility between previous versions of the Windows operating system. When enabled, communication on the network between DTCs can fall back to a non-authentication or non-encrypted communication if a secure communication channel cannot be established. This setting should be used if the DTC on the remote computer runs a Windows 2000 operating system or a Windows XP operating system earlier than SP2. This setting is also useful when the DTCs that are involved are located on computers that are in domains that do not have an established trust relationship or if the computers are part of a Windows workgroup.

AllowOnlySecureRpcCalls = 0

FallbackToUnsecureRPCIfNecessary = 0

TurnOffRpcSecurity = 1

 

 

2. 配置防火墙,同样也是对跑代码的机器sql所在的服务器进行配置

 

Windows Firewall -> Exceptions -> Add Program -> Browse -> "C:/WINDOWS/system32/msdtc.exe" -> OK

msdtc.exe 是windows 分布式服务程序。

 


 

博客园上的一个解决方法:

 

第一步:
--------------------
win2003缺省禁用远程/网络事务,启用的步骤如下:
启用网络 DTC 访问的步骤
1. 单击“开始”,指向“控制面板”,然后单击“添加/删除程序”。
2. 单击“添加/删除 Windows 组件”。
3. 选择“应用程序服务器”,然后单击“详细信息”。
4. 选择“启用网络 DTC 访问”,然后单击“确定”。
5. 单击“下一步”。
6. 单击“完成”。
7. 停止分布式事务协调器服务,然后重新予以启动。
8. 停止参与分布式事务的任何资源管理器服务(如 Microsoft SQL Server 或 Microsoft Message Queue Server),然后重新予以启动。
--------------------------------
第二步:
--------------------------------
设置MSDTC:控制面板->管理工具->组件服务->我的电脑->右键->属性->MSDTC->安全配置(Security Configuration)->打开Network DTC Access 同时打上
Allow Inbound ,Allow Outbound ,No Authentication Required,Enable Transaction Internet Protocol[TIP] Transaction
---------------------------------
第三步:将MSDTC程序添加入Windows防火墙

注意:两台参与分布事务的机器都需按如上操作设置。如果还有问题,则重启机器。


比我多了第一步,在我的试验中没有用到第一步。

有人说TransactionScope不支持跨域访问,在我的实验中,是可以跨域的。

 

关于TransactionScope有几点需要补充:

1. connection 须在TransactionScope内部打开且关闭

2. 执行sql时出现的异常要抛出到TransactionScope,以使得TransactionScope知道发生异常了,准备回滚

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的使用TransactionScope的代码示例,用于演示如何将多个数据库操作包装在一个事务中: ``` using System; using System.Data.SqlClient; using System.Transactions; class Program { static void Main() { // 创建一个连接字符串 string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=TestDB;Integrated Security=True"; // 创建一个事务范围对象 using (TransactionScope scope = new TransactionScope()) { try { // 执行第一个数据库操作 using (SqlConnection connection1 = new SqlConnection(connectionString)) { connection1.Open(); SqlCommand command1 = new SqlCommand("INSERT INTO Customers (Name) VALUES ('Customer 1')", connection1); command1.ExecuteNonQuery(); } // 执行第二个数据库操作 using (SqlConnection connection2 = new SqlConnection(connectionString)) { connection2.Open(); SqlCommand command2 = new SqlCommand("INSERT INTO Orders (CustomerId, OrderDate) VALUES ((SELECT TOP 1 Id FROM Customers ORDER BY Id DESC), GETDATE())", connection2); command2.ExecuteNonQuery(); } // 提交事务 scope.Complete(); } catch (Exception ex) { // 回滚事务 Console.WriteLine("Transaction rolled back: " + ex.Message); } } } } ``` 在这个示例中,我们使用了TransactionScope类来创建一个事务范围,然后在这个范围内执行了两个数据库操作。如果两个操作都成功执行,我们调用了TransactionScope的Complete方法来提交事务。如果其中任何一个操作失败,我们就会在catch块中回滚事务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值