前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Is this a MS EnterLib DAAB BUG or not?

Is this a MS EnterLib DAAB BUG or not?

作者头像
蒋金楠
发布2018-01-16 15:12:01
8680
发布2018-01-16 15:12:01
举报
文章被收录于专栏:大内老A大内老A

开门见山,使用MS Enterprise Library的DAAB(Data Access Application Block)获取数据时抛出异常。具体场景如下,通过Database对象的ExecuteReader执行两段Select语句,前一句是不合法的,后一句是正确的。为了避免第一次执行出错导致程序的终止,特意将其放到Try/Catch酷快中。两次数据库操作通过TrsanctionScope的形式纳入同一个Transaction中,具体的代码如下所示。

代码语言:js
复制
   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: } 

但是在执行第二个ExecuteReader方法的时候却抛出如下一个InvalidOperationException(如下图),错误消息为:“ExecuteReader requires an open and available Connection. The connection's current state is closed.”

原因出在这里:在ExecuteReader中,相应的ADO.NET代码放在try|catch中,当异常抛出后,相应的DbConnect会被关闭。但是由于在我的代码中,两次ExecuteReader的调用是在一个相同的Ambient Transaction中执行的,DAAB在内部采用相同的DbTransaction执行这两项操作,当执行第一项操作时,由于出现异常导致DbConnect关闭,使用相同DbConnect的第二项操作肯定会失败。

代码语言:js
复制
   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:  

我不清楚微软在设计的时候,是因为没有考虑到这种场景呢,还是不得以而为之,或者是出于其他因素的考虑,大家有何见解。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009-04-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档