---
title: "[原创] Is this a MS EnterLib DAAB BUG or not?"
author: "varsoft"
date: "2009-04-22T15:12:00Z"
tags: []
slug: "is-this-a-ms-enterlib-daab-bug-or-not"
draft: false
lastmod: "2009-04-22T15:12:00Z"
id: 871319
url: "https://varsoft.iteye.com/blog/871319"
---

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

```
   1: class Program
```

&lt;!--CRLF--&gt;

```
   2: {
```

&lt;!--CRLF--&gt;

```
   3:     static void Main()
```

&lt;!--CRLF--&gt;

```
   4:     {
```

&lt;!--CRLF--&gt;

```
   5:  
```

&lt;!--CRLF--&gt;

```
   6:         string invalidSql = "SELECT * FROM {InvalidTable}";
```

&lt;!--CRLF--&gt;

```
   7:         string validSql = "SELECT * FROM {ValidTable}";
```

&lt;!--CRLF--&gt;

```
   8:  
```

&lt;!--CRLF--&gt;

```
   9:  
```

&lt;!--CRLF--&gt;

```
  10:         Database db = DatabaseFactory.CreateDatabase();
```

&lt;!--CRLF--&gt;

```
  11:         using (TransactionScope scope = new TransactionScope())
```

&lt;!--CRLF--&gt;

```
  12:         {
```

&lt;!--CRLF--&gt;

```
  13:             DbCommand commandWithInvalidSql = db.GetSqlStringCommand(invalidSql);
```

&lt;!--CRLF--&gt;

```
  14:             DbCommand commandWithValidSql = db.GetSqlStringCommand(validSql);
```

&lt;!--CRLF--&gt;

```
  15:  
```

&lt;!--CRLF--&gt;

```
  16:             try
```

&lt;!--CRLF--&gt;

```
  17:             {
```

&lt;!--CRLF--&gt;

```
  18:                 db.ExecuteReader(commandWithInvalidSql);
```

&lt;!--CRLF--&gt;

```
  19:             }
```

&lt;!--CRLF--&gt;

```
  20:             catch
```

&lt;!--CRLF--&gt;

```
  21:             { }
```

&lt;!--CRLF--&gt;

```
  22:  
```

&lt;!--CRLF--&gt;

```
  23:             db.ExecuteReader(commandWithValidSql);
```

&lt;!--CRLF--&gt;

```
  24:         }
```

&lt;!--CRLF--&gt;

```
  25:     }
```

&lt;!--CRLF--&gt;

```
  26: } 
```

&lt;!--CRLF--&gt;

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

![](http://images.cnblogs.com/cnblogs_com/artech/ExecuteReader.GIF)

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

```
   1: public virtual IDataReader ExecuteReader(DbCommand command)
```

&lt;!--CRLF--&gt;

```
   2: {
```

&lt;!--CRLF--&gt;

```
   3:     ConnectionWrapper wrapper = GetOpenConnection(false);
```

&lt;!--CRLF--&gt;

```
   4:  
```

&lt;!--CRLF--&gt;

```
   5:     try
```

&lt;!--CRLF--&gt;

```
   6:     {
```

&lt;!--CRLF--&gt;

```
   7: //
```

&lt;!--CRLF--&gt;

```
   8: // JS-L: I moved the PrepareCommand inside the try because it can fail.
```

&lt;!--CRLF--&gt;

```
   9: //
```

&lt;!--CRLF--&gt;

```
  10: PrepareCommand(command, wrapper.Connection);
```

&lt;!--CRLF--&gt;

```
  11:  
```

&lt;!--CRLF--&gt;

```
  12: //
```

&lt;!--CRLF--&gt;

```
  13: // If there is a current transaction, we'll be using a shared connection, so we don't
```

&lt;!--CRLF--&gt;

```
  14: // want to close the connection when we're done with the reader.
```

&lt;!--CRLF--&gt;

```
  15: //
```

&lt;!--CRLF--&gt;

```
  16: if (Transaction.Current != null)
```

&lt;!--CRLF--&gt;

```
  17:     return DoExecuteReader(command, CommandBehavior.Default);
```

&lt;!--CRLF--&gt;

```
  18: else
```

&lt;!--CRLF--&gt;

```
  19:     return DoExecuteReader(command, CommandBehavior.CloseConnection);
```

&lt;!--CRLF--&gt;

```
  20:     }
```

&lt;!--CRLF--&gt;

```
  21:     catch
```

&lt;!--CRLF--&gt;

```
  22:     {
```

&lt;!--CRLF--&gt;

```
  23: wrapper.Connection.Close();
```

&lt;!--CRLF--&gt;

```
  24: throw;
```

&lt;!--CRLF--&gt;

```
  25:     }
```

&lt;!--CRLF--&gt;

```
  26: } 
```

&lt;!--CRLF--&gt;

```
  27: 
```

&lt;!--CRLF--&gt;

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

* * *