6

在 beta 测试期间,我们发现了连接池错误消息。因此,我一直在浏览代码并关闭未关闭的 SqlDataReader 对象。我需要知道的是如何关闭在 SqlDataSource 或 ObjectDataSource 标记的 SelectStatement 属性中指定的数据读取器(或者是否需要关闭)。如果不处理,会不会有连接泄漏?

提前致谢 !

4

7 回答 7

12

我倾向于使用“using”关键字,尤其是在处理打开和关闭与数据库的连接时。“使用”是 Dispose 模式的快捷方式——这里是 MSDN 文章的链接,这里是一个有用的博客条目的链接,其中包含概述。

于 2008-10-20T18:48:17.530 回答
4

To improve performance of Close()/Dispose() consider calling Cancel() on the associated command object before disposing or closing the reader, especially when you didn't reach the end of the record set.

For example:

            using (var cmd = ... ))
            {
                using (var reader = (DbDataReader) cmd.ExecuteReader())
                {
                    try
                    {
                        ConsumeData(reader); // may throw
                    }
                    catch(Exception)
                    {
                        cmd.Cancel();
                        throw;
                    }
                }
            }
于 2009-10-17T08:05:27.183 回答
2

我的理解是,使用SqlDataSource,为您执行连接管理,您无所畏惧。

ObjectDataSource首先不直接与数据库对话,因此它是安全的——只要底层对象正确执行其连接和读取器管理。

正如其他人所提到的,Close()并且using是您与ObjectDataSource.

我的预感是,如果你有效地清理了代码库,你可能已经根除了这个问题。

于 2008-10-20T18:57:49.540 回答
2

We had the same problems here in a production environment.

Solved the issue. The problem first was, there were no using statements at all in my code. (It was build a few years ago, with some less knowledge).

I then tried putting the SqlDataSource in a using clause. But this didn't help either.

The trick here is, just like tvanfosson and Mischa are suggesting, putting the reader in a using clause. This is the object that actually closes the connection.

The number of connections shrunk to the minimum pool size of 10 at medium load.

于 2011-01-18T16:37:12.367 回答
0

调用 .Dispose() 应该处理清理并释放任何持有的资源,但是当对象完成从 reader 读取时,也应该调用 .Close() 方法

于 2008-10-20T18:51:56.337 回答
0

我相信 SqlDataSource 会处理它自己的连接/阅读器问题,所以不用担心。至于您的手动连接,我发现这种模式在过去很有用:

   using (SqlConnection connection = new SqlConnection(connectionString))
   {
      try
      {
         SqlCommand command = connection.CreateCommand();
         command.CommandText = ...

         connection.Open();
         using (SqlDataReader reader = command.ExecuteReader())
         {
            do
            {
               while (reader.Read())
               {
                  ... handle each row ...
               }
            } while (reader.NextResult());
         }
      }
      catch (Exception ex)
      {
          ... error handling ...
      }
      finally
      {
         if (connection != null && connection.State == ConnectionState.Open)
         {
            connection.Close();
         }
      }
  }
于 2008-10-20T21:39:30.820 回答
-1

我同意,对于 ObjectDataSource,关闭应由其 Select 方法处理。我的 ObjectDataSource Select 方法返回一个 SqlDataReader。我担心的是......将 SqlDataReader 返回到 UI 后关闭时是否会变得无用。例如,请参阅以下示例代码。我没有尝试过,也不想在这个开发阶段这样做。

SqlDataReader MySelectMethod(){
   SqlDataReader dr = null;
   try{
      dr = DBObject.GetDataReader();
      return dr;
   }
   finally{
      dr.Close();
   }
}

感谢您迄今为止收到的所有意见!

............

My understanding is that with SqlDataSource, connection management is performed for you, and you have nothing to fear.

ObjectDataSource doesn't talk to the database directly in the first place, so it will be safe -- as long as the underlying object performs its connection and reader management correctly.

As others have mentioned, Close() and using are your friends for the classes you use with ObjectDataSource

.

于 2008-10-21T00:21:10.150 回答