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知道发生异常了,准备回滚