標籤:des io os ar for sp 資料 art on
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知道發生異常了,準備復原
TransactionScope 之分布式配置