Is this a MS EnterLib DAAB BUG or not?
但是在執(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)題。
- 上一篇: Rose与PowerDesigner:两
- 下一篇: [转]使用Ant进行ssh和scp操作