前言

這兩天協助處理幾個狀況複雜又緊急的 Oracle 版本茶包,感覺自己診斷 ODP.NET 版本問題的功力連升好幾級。(揠苗助長式的成長,我不想要呀~) 覺得自己走了狗屎運,幾天前好巧不巧讓我搞懂 ASP.NET /bin/Oracle.DataAccess.dll 其實是幌子的事實,還想到用 Assembly.CodeBase 找出實際使用版本的技巧,不然沒掌握版本,這回只怕連怎麼死的都不知道。 所以,接下來將有 ODP.NET 茶包文連發,敬請期待~

本文開始

同事報案,在新環境架設網站,有段 TransactionScope 包 Oracle 操作的功能噴出分散式交易錯誤:

System.Data.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: The Promote method returned an invalid value for the distributed transaction.

依訊息研判與分散式交易設定有關,但有個小阻礙。觸發錯誤的功能埋得頗深,要十幾個步驟才能重現錯誤,觀察與驗證都很困難。故第一步要找出快速重現問題的方法以爭取時效,之前介紹過使用 Inline ASPX 在正式環境快速偵錯 MSDTC 的技巧 派上用場, 不用改專案編譯及部署,寫個 test.aspx 檔交給管理人員丟上伺服器,瀏覽網頁即可觀察錯誤訊息並驗證問題是否修復: (註:範例順便分享 Oracle GUID 產生函式 sys_guid() )

<%@Page Language="C#"%>
<%@Import Namespace="System.Transactions"%>
<%@Import Namespace="Oracle.DataAccess.Client"%>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        var cs = "data source=my-ora;user id=user;password=pwd";
		using (var tx = new TransactionScope()) {
			ExecQuery(cs);
			//經實測,11.2 連線字串相同的兩條連線也觸發分散式交易
			//12.1 連線字串要故意做出差異才會進入分散式交易模式
			cs += ";Pooling=false";
			ExecQuery(cs);
			Response.Write(
				"<li>TransId=" +
				Transaction.Current.TransactionInformation.DistributedIdentifier);
		}
    }
	
	void ExecQuery(string cs) 
	{
		using (var cn = new OracleConnection(cs))
		{
			cn.Open();
			var cmd = cn.CreateCommand();
			cmd.CommandText = "select sys_guid() from dual";
			var dr = cmd.ExecuteReader();
			dr.Read();
			Response.Write("<li>" + new Guid((byte[])dr[0]));
		}
	}
</script>

正常情況應看到以下結果:

若查詢可執行但未啟動分散式交易,則 DistributedIdentifier 為空白 GUID:

若分散式交易安裝設定有誤,則會出現文章前面提到的 The Promote method returned an invalid value for the distributed transaction. 錯誤。

有了工具,經過一番調查,查出其 Unmanaged ODP.NET 版本為 2.121.1.0 (用前幾天介紹過的 Assembly.CodeBase 檢查法確認), 該主機所安裝的 OracleMTSRecoveryService 是 11.2 版 (我是靠 Registry 確認,如下圖), 試著用 BidingRedirect 技巧將版本導向 2.112.3.0 後問題排除。

故障雖已排除卻留下謎團。因為在測試環境實測:不管 2.121.1、4.121.1、2.112.3 都能搭配 11.2 版 OracleMTSRecoveryService 啟用分散式交易,該主機在早先排除其他問題的過程 陸續安裝了多個版本 Oracle Client (12.1、11.2.0.1、11.2.0.3),且順序有點混亂,有可能是造成問題的原因,但該主機另有重任不適合做實驗,此一疑問先歸入 X 檔案,有緣再查。

另記本次得到的新知識:ODP.NET 11.2 只要在 TransactionScope 中開啟兩條連線字串相同的連線也會啟動分散式交易,而 12.1 則跟 SQL 2008 一樣,連線字串不同才會啟動分散式交易。

Showing how to use inline ASPX to reproduce Oracle MSDTC issue with TransactionScope. Including two OracleConnection with different connection string can trigger distributed transaction, then you can check TransactionInformation.DistributedIdentifier to determine if distributed transaction is enabled.


Comments

Be the first to post a comment

Post a comment