CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

这样写Dispose()释放资源合适不合适?

楼主SystemArchitect(SystemArchitect)2006-05-02 12:15:21 在 .NET技术 / C# 提问

public   class   myclass   :   IDisposable  
          {  
                  private   OleDbCommand   selectCommand;  
                  private   OleDbCommand   insertCommand;  
                  private   OleDbCommand   updateCommand;  
                  private   OleDbCommand   deleteCommand;  
                  private   OleDbCommand   storedCommand;  
                  private   OleDbConnection   connectionObj;  
                  private   bool   isDisposed   =   false;    
   
  ............  
  ...........  
   
  public   void   Dispose()  
                  {  
                          Dispose(true);  
                          GC.SuppressFinalize(true);   //   as   a   service   to   those   who   might   inherit   from   us  
                  }  
                   
                  protected   virtual   void   Dispose(bool   disposing)  
                  {  
                          if   (!isDisposed)  
                          {  
                                  if   (disposing)  
                                  {  
                                          if   (selectCommand   !=   null)  
                                          {  
                                                  selectCommand.Dispose();  
                                                  selectCommand   =   null;  
                                          }  
                                          if   (updateCommand   !=   null)  
                                          {  
                                                  updateCommand.Dispose();  
                                                  updateCommand   =   null;  
                                          }  
                                          if   (insertCommand   !=   null)  
                                          {  
                                                  insertCommand.Dispose();  
                                                  insertCommand   =   null;  
                                          }  
                                          if   (deleteCommand   !=   null)  
                                          {  
                                                  deleteCommand.Dispose();  
                                                  deleteCommand   =   null;  
                                          }  
                                          if   (dataAdapter   !=   null)  
                                          {  
                                                  dataAdapter.Dispose();  
                                                  dataAdapter   =   null;  
                                          }  
                                  }  
                                  if   (connectionObj   !=   null)//connectionObj是该放在这吗?  
                                  {  
                                          connectionObj.Dispose();  
                                          connectionObj   =   null;  
                                  }  
                          }  
                          isDisposed   =   true;  
                  }  
  } 问题点数:20、回复次数:10Top

1 楼yurow(路漫漫其修远兮,吾将上下而爬楼梯!)回复于 2006-05-02 12:28:11 得分 5

OleDbConnection   对象可以在这里被释放,如果你程序中一个方法内使用的同一个OleDbConnection   只有一次  
   
  比如:  
  conn.Open();  
  //操作  
  conn.Close();  
  conn.Disponse();  
   
  如果一个OleDbConnection实例调用两次那就会出问题  
   
  比如  
  Class   a  
  {  
            void   b()  
            {  
                      //代码  
                      conn.Disponse();  
            }  
   
            void   c()  
            {  
                      //代码  
                      conn.Disponse();  
            }  
  }  
   
  class   x  
  {  
            a   Mya   =   new   a();  
            a.b();  
            a.c();//出错,conn对象被释放,OleDbConnection对象不存在  
   
  }Top

2 楼SystemArchitect(SystemArchitect)回复于 2006-05-02 18:04:25 得分 0

我想知道OleDbConnection是托管资源还是非托管资源?Top

3 楼yurow(路漫漫其修远兮,吾将上下而爬楼梯!)回复于 2006-05-02 18:58:20 得分 0

是托管的Top

4 楼diandian82(点点(nothing))回复于 2006-05-02 22:58:49 得分 0

只能说,没必要。Top

5 楼AhBian(阿扁)回复于 2006-05-02 23:44:46 得分 5

conn.Close();  
  conn.Dispose();  
   
  以上二句方法调用是不必要的。而且建议只需执行一次   Close   即可。Close   比   Dipose   好,对于   conn   来说,不信,你查   MSDN,上面有明确说明。  
   
  及时释放非托管资源,是相当重要的。Top

6 楼roydu(水源倒爷)回复于 2006-05-03 00:12:59 得分 0

同意   AhBian(阿扁)   的Top

7 楼BlueDog(身正方能顶天立地,心和才可容士纳物。)回复于 2006-05-04 23:10:18 得分 5

1、CLOSE在内部也是调用Dispose,没有本质区别,只是更加符合程序逻辑  
   
  2、ADO.NET的与ADO的一个最大区别是取到数据后马上断开,减少连接占用,  
        可以参考DAAB的代码  
   
  Top

8 楼san12655874(小三33)回复于 2006-05-04 23:53:27 得分 0

同意以上2楼的Top

9 楼namhyuk(namhyuk)回复于 2006-05-05 01:10:10 得分 0

至少Connection的Close和Dispose应该是这样子。  
  1、Calling   Close   on   a   connection   object   enables   the   underlying   connection   to   be   pooled.  
   
  2、Calling   Dispose   on   a   connection   object   alleviates   the   need   for   you   to   call   Close   on   it   explicitly.(也就是说Dispose会自动帮你调用Close)。It   not   only   ensures   that   the   underlying   connection   can   be   pooled,   but   it   also   makes   sure   that   allocated   resources   can   now   be   garbage   collected.  
   
  3、When   the   open   connections   fall   out   of   scope,   they   won't   be   garbage   collected   for   a   relatively   long   time   because   the   connection   object   itself   doesn't   occupy   that   much   memory-and   the   lack   of   memory   is   the   sole   criterion   for   the   garbage   collector   to   kick   in   and   do   its   work.  
   
  In   short,   Dispose   is   the   best   option   as   it   helps   garbage   collection   and   connection   pooling.   Close   is   second   best   option   as   it   helps   only   connection   pooling.  
   
  其它:The   ADO.NET   uses   the   Dispose   method   to   clean   up   unallocated   resources.   In   addition   to   cleaning   up   unallocated   resources,   Dispose   calls   Close   on   a   connection   object   and   makes   it   ready   for   reuse   in   a   connection   pool.   In   addition   to   calling   Close,   Dispose   does   a   little   more   housekeeping   work   than   Close   might   do.   Dispose   cleans   the   internal   collections   to   clear   out   various   settings,   such   as   the   connection   string   on   a   connection   object,   so   it   allows   the   garbage   collector   to   reuse   the   memory   used   by   the   actual   connection   object...  
   
  下面的例子并没有显示执行Connection.Close()  
  using(SqlConnection   conn   =   new   SqlConnection(connString))  
  {  
          SqlCommand   cmd   =   new   SqlCommand("select   *   from   TABLE",   conn);  
           
          conn.Open();  
           
          SqlDataReader   sqlDr   =   cmd.ExecuteReader(CommandBehavior.CloseConnection);  
          if(sqlDr.HasRows)  
          {  
                  foreach(DbDataRecord   rec   in   sqlDr)  
                  {  
                          dbRecordsHolder.Add(rec);   //   dbREcordsHoler   is   an   ArrayList  
                  }  
          }  
  ]   //注意:   conn.Dispose   is   called   automatically,   而Dispose会自动调用Close  
   
  另外一个问题是:CommandBehavior.Close也会自动关闭DataReader及其Connection,也就是说这段代码会有两次Close操作。这个问题在某个论坛看过有人讨论,不过当时没有来得及看。  
   
   
  Top

10 楼antoniusguo(anton)回复于 2006-05-06 20:11:33 得分 5

我想说一下  
  public   void   Dispose()  
                  {  
                          Dispose(true);  
                          GC.SuppressFinalize(true);   //   as   a   service   to   those   who   might   inherit   from   us  
                  }  
  这个方法  
  SuppressFinalize  
  是指取消为这个类执行Finalize方法  
  所以如果显示调用Dispose方法的话就不需要在垃圾收集的时候执行Finalize   析构函数  
  这个方法的参数是指要取消的对象  
  我想这里调用的话应该是  
  GC.SuppressFinalize(this);Top

相关问题

关键词

得分解答快速导航

  • 帖主:SystemArchitect
  • yurow
  • AhBian
  • BlueDog
  • antoniusguo

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo