測試DTC設定-使用Mini C# Lab

每次安裝Web測試環境的一項重要檢查工作,是要確認Web與SQL間的DTC連線暢通,在經驗裡這是件眉眉角角很多的任務,光從我過去針對它寫過的KB、文章數量就可見一斑。

測試DTC通不通的方法,除了直接執行引用DTC的程式之外,還可以用MS的DTCPing工具。不過,這次介紹如何使用我寫的小工具Mini C# Lab來測試DTC。

測試原理請大家參考".NET 分散式交易程式開發FAQ(PDF)"一文,用Mini C# Lab跑以下的小程式,在TransactionScope中對SQL建立兩條連線會觸發使用DTC,因此若沒設好,第二個查詢會傳回錯誤。(【2010-11-12更新】以下寫法應用於SQL 2008時,會因連線字串相同而不觸發DTC,改良做法請見這裡)

using System;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
//REFDLL System.Data;System.Xml;System.Transactions
 
public class CSharpLab
{
    private static void querySqlServer() 
    {
        string cnStr =  "Data Source=theSql;User Id=theUser;Password=thePasswd;";
        using (SqlConnection cn = new SqlConnection(cnStr))
        {
            SqlCommand cmd = new SqlCommand("SELECT getdate() as D", cn);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            Console.WriteLine(dr["D"]);
            cn.Close();
        }
    }
 
    public static void Test()
    {
        using (TransactionScope tx=new TransactionScope()) 
        {   
            querySqlServer();
            querySqlServer();
            tx.Complete();
        }
    }
}

 

以本次測試為例,因為"Network DTC Access"沒啟動,得到以下錯誤:

意外發現,錯誤訊息還挺明確的,本機Network DTC Access沒開時長這樣:

ERROR: Runtime error!
Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool

如果是遠端那台沒開啟長這樣:

ERROR: Runtime error!
The partner transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D025)

常被雞同鴨講聲東擊西指鹿為馬不知所云的錯誤訊息氣到吐血,看到這麼精確的訊息,就甘心A。

歡迎推文分享:
Published 22 February 2009 09:17 AM 由 Jeffrey
Filed under: , ,
Views: 17,986



意見

# leeyun said on 07 October, 2009 10:10 AM

您好,我最近做DTC方面的一个项目,碰见一个问题,向您请教。

code总是报“与基础事务管理器的通信失败”

用msdtcping测试通过,使用您的mini lab也是报上面的错误

谢谢~

mini lab 的cod如下:

public class CSharpLab

{

   private static void querySqlServer()

   {

       string cnStr =  "Data Source=192.168.10.166;Initial Catalog=ATC_WOS;Persist Security Info=True;User ID=sa;Password=sunfast";

       using (SqlConnection cn = new SqlConnection(cnStr))

       {

           SqlCommand cmd = new SqlCommand("SELECT getdate() as D", cn);

           cn.Open();

           SqlDataReader dr = cmd.ExecuteReader();

           dr.Read();

           Console.WriteLine(dr["D"]);

           cn.Close();

       }

   }

   public static void Test()

   {

       using (TransactionScope tx=new TransactionScope())

       {  

           querySqlServer();

           querySqlServer();////注释掉该行可以通过

           tx.Complete();

       }

   }

}

# Jeffrey said on 07 October, 2009 03:27 PM

to leeyun, 使用分散式交易要注意的小地方挺多的,沒有設定好就會得到以上的訊息。我在blog2.darkthread.net/.../distributed-transactoin-with-dotnet-faq.aspx 一文中有整理我遇到的一些常見問題、檢查步驟及解決方式,你可以參考看看。

# leeyun said on 07 October, 2009 09:10 PM

谢谢您的回复,我看过你的相关的所有文章,那个faq也全看过,

其我这边得到的错误,英文是"Communication with the underlying transaction manager has failed. ".我用dtcping测试是正确的啊,所以不太明白是哪儿有问题。

我这边的环境是 "web服务器 win2003 sp2" + "DB服务器 win2003 sp3",  两边的防火墙全关闭了,对于数据的操作都是在DB服务器的一个数据库上面,只是多次open,close 数据库的连接

# Jeffrey said on 07 October, 2009 11:36 PM

to leeyun, TransactionScope中用不同連線open同一資料庫,就會構成啟用分散式交易的條件,因此,第二次的失敗正是因為企圖呼叫DTC沒成功的緣故。dtcping從兩台機器互相測試都成功嗎? 如果是這樣還無法連通就挺少見的。

我再提供一個測試,請從web上開cmd.exe測ping db的機器名稱,從db上試ping web的機器名稱,一定要用機器名稱,不可用IP取代。我的經驗中,只要任一方無法透過這種方式找到到對方,就可能導致DTC失敗。

# leeyun said on 08 October, 2009 02:07 AM

这个是Web SERVER 中 dtcping记录的log

Platform:Windows 2003

Windows 2003 environment is detected:

Reading MSDTC settings from local registry:

Security settings for DTC under Windows 2003

NetworkDtcAccess            :true

NetworkDtcAccessAdmin       :true

NetworkDtcAccessClients     :true

NetworkDtcAccessTransactions:true

NetworkDtcAccessTip         :true

XaTransactions              :true

DomainControllerState       :false

AccountName                 :NT Authority\NetworkService

TurnOffRpcSecurity          :true

IP Configure Information

Host Name . . . . . . . . . : lizhongbo

DNS Servers . . . . . . . . : 192.168.10.53

                                     202.96.199.133

Node Type . . . . . . . . . :

NetBIOS Scope ID. . . . . . :

IP Routing Enabled. . . . . : no

WINS Proxy Enabled. . . . . : no

NetBIOS Resolution Uses DNS : no

Ethernet adapter {24DAC1AD-E48E-4972-9972-C0350E2D4849}:

Description . . . . . . . . : Marvell Yukon 88E8057 PCI-E Gigabit Ethernet Controller

Physical Address. . . . . . : 00-01-6C-41-F6-09

DHCP Enabled. . . . . . . . : no

IP Address. . . . . . . . . : 192.168.80.187

Subnet Mask . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . : 192.168.80.254

DHCP Server . . . . . . . . : 255.255.255.255

Primary WINS Server . . . . : 0.0.0.0

Secondary WINS Server . . . : 0.0.0.0

Lease Obtained. . . . . . . : Thu Jan 01 00:00:00 1970

Lease Expires . . . . . . . : Thu Jan 01 00:00:00 1970

10-08, 11:29:41.203-->Error(0x2) at NetName.cpp @245

10-08, 11:29:41.203-->-->fopen for host file

10-08, 11:29:41.203-->-->2(The system cannot find the file specified.)

++++++++++++hosts      ++++++++++++

127.0.0.1       localhost

192.168.10.166 test-atc

++++++++++++++++++++++++++++++++++++++++++++++

    DTCping 1.9 Report for LIZHONGBO  

++++++++++++++++++++++++++++++++++++++++++++++

RPC server is ready

10-08, 11:30:06.343-->RPC server:LIZHONGBO received following information:

Network Name: lizhongbo

Source  Port: 1484

Partner LOG: TEST-ATC412.log

Partner CID: 52DC8165-A3C4-4751-8D7B-967D88C8D0CB

++++++++++++Start Reverse Bind Test+++++++++++++

Received Bind call from TEST-ATC

Network Name: lizhongbo

Source  Port: 1484

Hosting Machine:LIZHONGBO

10-08, 11:30:06.359-->Trying to Reverse Bind to TEST-ATC...

Test Guid:52DC8165-A3C4-4751-8D7B-967D88C8D0CB

Name Resolution:

TEST-ATC-->192.168.10.166-->test-atc

Reverse Binding success: LIZHONGBO-->TEST-ATC

++++++++++++Reverse Bind Test ENDED++++++++++

10-08, 11:30:06.421-->Called POKE from Partner:TEST-ATC

Network Name: lizhongbo

Source  Port: 1484

Hosting Machine:LIZHONGBO

++++++++++++Validating Remote Computer Name++++++++++++

10-08, 11:32:15.562-->Start DTC connection test

Name Resolution:

test-atc-->192.168.10.166-->test-atc

10-08, 11:32:15.562-->Start RPC test (LIZHONGBO-->test-atc)

RPC test is successful

Partner's CID:52DC8165-A3C4-4751-8D7B-967D88C8D0CB

++++++++++++RPC test completed+++++++++++++++

++++++++++++Start DTC Binding Test +++++++++++++

Trying Bind to test-atc

10-08, 11:32:15.578-->LIZHONGBO Initiating DTC Binding Test....

Test Guid:1753F053-322F-4F4C-8098-6B334EAF5F1B

Received reverse bind call from test-atc

Network Name: lizhongbo

Source  Port: 1484

Hosting Machine:LIZHONGBO

Binding success: LIZHONGBO-->test-atc

++++++++++++DTC Binding Test END+++++++++++++

# leeyun said on 08 October, 2009 02:08 AM

DB Server上面 dtcping记录的log

Platform:Windows 2003

Windows 2003 environment is detected:

Reading MSDTC settings from local registry:

Security settings for DTC under Windows 2003

NetworkDtcAccess            :true

NetworkDtcAccessAdmin       :true

NetworkDtcAccessClients     :true

NetworkDtcAccessTransactions:true

NetworkDtcAccessTip         :true

XaTransactions              :true

DomainControllerState       :false

AccountName                 :NT Authority\NetworkService

TurnOffRpcSecurity          :false

IP Configure Information

Host Name . . . . . . . . . : test-atc

DNS Servers . . . . . . . . : 192.168.10.53

                                     202.96.199.133

Node Type . . . . . . . . . :

NetBIOS Scope ID. . . . . . :

IP Routing Enabled. . . . . : no

WINS Proxy Enabled. . . . . : no

NetBIOS Resolution Uses DNS : no

Ethernet adapter {6B7108C8-74ED-421F-9F85-B726B5630468}:

Description . . . . . . . . : Broadcom NetLink (TM) Gigabit Ethernet

Physical Address. . . . . . : 00-1C-25-C6-62-A1

DHCP Enabled. . . . . . . . : no

IP Address. . . . . . . . . : 192.168.10.166

Subnet Mask . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . : 192.168.10.254

DHCP Server . . . . . . . . : 255.255.255.255

Primary WINS Server . . . . : 0.0.0.0

Secondary WINS Server . . . : 0.0.0.0

Lease Obtained. . . . . . . : Thu Jan 01 00:00:00 1970

Lease Expires . . . . . . . : Thu Jan 01 00:00:00 1970

10-07, 23:50:54.515-->Error(0x2) at NetName.cpp @245

10-07, 23:50:54.515-->-->fopen for host file

10-07, 23:50:54.515-->-->2(The system cannot find the file specified.)

++++++++++++hosts      ++++++++++++

127.0.0.1       localhost

192.168.80.141    rqwveutgj3txzdq

192.168.10.54  ad2

192.168.80.187 lizhongbo

++++++++++++++++++++++++++++++++++++++++++++++

    DTCping 1.9 Report for TEST-ATC  

++++++++++++++++++++++++++++++++++++++++++++++

RPC server is ready

++++++++++++Validating Remote Computer Name++++++++++++

10-07, 23:51:02.546-->Start DTC connection test

Name Resolution:

lizhongbo-->192.168.80.187-->lizhongbo

10-07, 23:51:02.546-->Start RPC test (TEST-ATC-->lizhongbo)

RPC test is successful

Partner's CID:1753F053-322F-4F4C-8098-6B334EAF5F1B

++++++++++++RPC test completed+++++++++++++++

++++++++++++Start DTC Binding Test +++++++++++++

Trying Bind to lizhongbo

10-07, 23:51:02.562-->TEST-ATC Initiating DTC Binding Test....

Test Guid:52DC8165-A3C4-4751-8D7B-967D88C8D0CB

Received reverse bind call from lizhongbo

Network Name: test-atc

Source  Port: 1564

Hosting Machine:TEST-ATC

Binding success: TEST-ATC-->lizhongbo

++++++++++++DTC Binding Test END+++++++++++++

10-07, 23:51:38.234-->RPC server:TEST-ATC received following information:

Network Name: test-atc

Source  Port: 1564

Partner LOG: LIZHONGBO1368.log

Partner CID: 1753F053-322F-4F4C-8098-6B334EAF5F1B

# leeyun said on 08 October, 2009 02:11 AM

再次谢谢您的回复,如果您不介意的话,可以mail回复我。

我的mail:persialee@hotmail.com

谢谢~

                        LeeYun

你的看法呢?

(必要的) 
(必要的) 
(選擇性的)
(必要的) 
(提醒: 因快取機制,您的留言幾分鐘後才會顯示在網站,請耐心稍候)

5 + 3 =

搜尋

Go

<February 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567
 
RSS
創用 CC 授權條款
【廣告】
twMVC

Tags 分類檢視
關於作者

一個醉心技術又酷愛分享的Coding魔人,十年的IT職場生涯,寫過系統、管過專案, 也帶過團隊,最後還是無怨無悔地選擇了技術鑽研這條路,近年來則以做一個"有為的中年人"自許。

文章典藏
其他功能

這個部落格


Syndication