日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Is this a MS EnterLib DAAB BUG or not?

發(fā)布時(shí)間:2024/4/15 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Is this a MS EnterLib DAAB BUG or not? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
開門見山,使用MS Enterprise LibraryDAABData Access Application Block)獲取數(shù)據(jù)時(shí)拋出異常。具體場(chǎng)景如下,通過(guò)Database對(duì)象的ExecuteReader執(zhí)行兩段Select語(yǔ)句,前一句是不合法的,后一句是正確的。為了避免第一次執(zhí)行出錯(cuò)導(dǎo)致程序的終止,特意將其放到Try/Catch酷快中。兩次數(shù)據(jù)庫(kù)操作通過(guò)TrsanctionScope的形式納入同一個(gè)Transaction中,具體的代碼如下所示。? 1: class Program 2: { 3: static void Main() 4: { 5: 6: string invalidSql = "SELECT * FROM {InvalidTable}"; 7: string validSql = "SELECT * FROM {ValidTable}"; 8: 9: 10: Database db = DatabaseFactory.CreateDatabase(); 11: using (TransactionScope scope = new TransactionScope()) 12: { 13: DbCommand commandWithInvalidSql = db.GetSqlStringCommand(invalidSql); 14: DbCommand commandWithValidSql = db.GetSqlStringCommand(validSql); 15: 16: try 17: { 18: db.ExecuteReader(commandWithInvalidSql); 19: } 20: catch 21: { } 22: 23: db.ExecuteReader(commandWithValidSql); 24: } 25: } 26: }

但是在執(zhí)行第二個(gè)ExecuteReader方法的時(shí)候卻拋出如下一個(gè)InvalidOperationException(如下圖),錯(cuò)誤消息為:“ExecuteReader requires an open and available Connection. The connection's current state is closed.?

原因出在這里:在ExecuteReader中,相應(yīng)的ADO.NET代碼放在try|catch中,當(dāng)異常拋出后,相應(yīng)的DbConnect會(huì)被關(guān)閉。但是由于在我的代碼中,兩次ExecuteReader的調(diào)用是在一個(gè)相同的Ambient Transaction中執(zhí)行的,DAAB在內(nèi)部采用相同的DbTransaction執(zhí)行這兩項(xiàng)操作,當(dāng)執(zhí)行第一項(xiàng)操作時(shí),由于出現(xiàn)異常導(dǎo)致DbConnect關(guān)閉,使用相同DbConnect的第二項(xiàng)操作肯定會(huì)失敗。

1: public virtual IDataReader ExecuteReader(DbCommand command) 2: { 3: ConnectionWrapper wrapper = GetOpenConnection(false); 4: 5: try 6: { 7: // 8: // JS-L: I moved the PrepareCommand inside the try because it can fail. 9: // 10: PrepareCommand(command, wrapper.Connection); 11: 12: // 13: // If there is a current transaction, we'll be using a shared connection, so we don't 14: // want to close the connection when we're done with the reader. 15: // 16: if (Transaction.Current != null) 17: return DoExecuteReader(command, CommandBehavior.Default); 18: else 19: return DoExecuteReader(command, CommandBehavior.CloseConnection); 20: } 21: catch 22: { 23: wrapper.Connection.Close(); 24: throw; 25: } 26: } 27:?

我不清楚微軟在設(shè)計(jì)的時(shí)候,是因?yàn)闆]有考慮到這種場(chǎng)景呢,還是不得以而為之,或者是出于其他因素的考慮,大家有何見解。


作者:Artech
出處:http://artech.cnblogs.com
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。

總結(jié)

以上是生活随笔為你收集整理的Is this a MS EnterLib DAAB BUG or not?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。