最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

asp教程:深入解析Close()和Dispose()的区别

日期:07-27    来源:中国设计秀    作者:cnwebshow.com

很多人都认为Close()方法内部会调用Dispose()方法,所以并没有本质的区别!实际上这个看法不是很准确,对有ntX中国设计秀
ntX中国设计秀
些类来说,的确Close()和Dispose()没有本质区别,但是对有些类来说并非如此!ntX中国设计秀
首先,让我们看看我们最常使用的SqlConnection的Close()方法和Dispose()方法的区别:ntX中国设计秀
SqlConnection类的Dispose()方法是继承于Component类的,源代码是这样的:ntX中国设计秀
        public void Dispose() { ntX中国设计秀
            Dispose(true); //调用Dispose的一个带参数的重载ntX中国设计秀
            GC.SupPRessFinalize(this);  //请求系统不要调用指定对象的终结器。ntX中国设计秀
        }ntX中国设计秀
        protected virtual void Dispose(bool disposing) {ntX中国设计秀
            if (disposing) { ntX中国设计秀
                lock(this) {ntX中国设计秀
                    if (site != null && site.Container != null) {ntX中国设计秀
                        site.Container.Remove(this);ntX中国设计秀
                    } ntX中国设计秀
                    if (events != null) {ntX中国设计秀
                        EventHandler handler = (EventHandler)events[EventDisposed]; ntX中国设计秀
                        if (handler != null) handler(this, EventArgs.Empty); ntX中国设计秀
                    }ntX中国设计秀
                } ntX中国设计秀
            }ntX中国设计秀
        }ntX中国设计秀
SqlConnection类的Close()方法在MSDN中的说明是这样的:ntX中国设计秀
关闭与数据库的连接。这是关闭任何打开连接的首选方法。 如果 SqlConnection 超出范围,则不会将其关闭。因ntX中国设计秀
ntX中国设计秀
此,必须通过调用 Close 或 Dispose 显式关闭该连接。Close 和 Dispose 在功能上等效。如果连接池值 ntX中国设计秀
ntX中国设计秀
Pooling 设置为 true 或 yes,则基础连接将返回到连接池。另一方面,如果 Pooling 设置为 false 或 no,则ntX中国设计秀
ntX中国设计秀
会关闭到服务器的基础连接。 ntX中国设计秀
看说明好象是Close()方法和Dispose()方法是类似的,实际上只是在关闭连接这个功能上等效,让我们看看ClosentX中国设计秀
ntX中国设计秀
()方法的源代码:ntX中国设计秀
        override public void Close() { ntX中国设计秀
            IntPtr hscp;ntX中国设计秀
            Bid.ScopeEnter(out hscp, "<sc.SqlConnection.Close|API> %d#" , ObjectID); ntX中国设计秀
            try {ntX中国设计秀
                SqlStatistics statistics = null;ntX中国设计秀
ntX中国设计秀
                RuntimeHelpers.PrepareConstrainedRegions(); ntX中国设计秀
                try {ntX中国设计秀
#if DEBUG ntX中国设计秀
                    object initialReliabilitySlotValue = Thread.GetData(TdsParser.ReliabilitySlot); ntX中国设计秀
ntX中国设计秀
                    RuntimeHelpers.PrepareConstrainedRegions(); ntX中国设计秀
                    try {ntX中国设计秀
                        Thread.SetData(TdsParser.ReliabilitySlot, true);ntX中国设计秀
#endif //DEBUGntX中国设计秀
                        statistics = SqlStatistics.StartTimer(Statistics); ntX中国设计秀
ntX中国设计秀
                        // The lock here is to protect against the command.cancel / connection.close ntX中国设计秀
ntX中国设计秀
race condition ntX中国设计秀
                        // The SqlInternalConnectionTds is set to OpenBusy during close, once this ntX中国设计秀
ntX中国设计秀
happens the cast below will fail and ntX中国设计秀
                        // the command will no longer be cancelable.  It might be desirable to be ntX中国设计秀
ntX中国设计秀
able to cancel the close opperation, but this isntX中国设计秀
                        // outside of the scope of Whidbey RTM.  See (SqlCommand::Cancel) for other ntX中国设计秀
ntX中国设计秀
lock. ntX中国设计秀
                        lock (InnerConnection) {ntX中国设计秀
                            InnerConnection.CloseConnection(this, ConnectionFactory);ntX中国设计秀
                        }ntX中国设计秀
                        // does not require GC.KeepAlive(this) because of OnStateChange ntX中国设计秀
ntX中国设计秀
                        if (null != Statistics) { ntX中国设计秀
                            ADP.TimerCurrent(out _statistics._closeTimestamp); ntX中国设计秀
                        }ntX中国设计秀
#if DEBUG ntX中国设计秀
                    }ntX中国设计秀
                    finally {ntX中国设计秀
                        Thread.SetData(TdsParser.ReliabilitySlot, initialReliabilitySlotValue);ntX中国设计秀
                    } ntX中国设计秀
#endif //DEBUGntX中国设计秀
                } ntX中国设计秀
                catch (System.OutOfMemoryException e) { ntX中国设计秀
                    Abort(e);ntX中国设计秀
                    throw; ntX中国设计秀
                }ntX中国设计秀
                catch (System.StackOverflowException e) {ntX中国设计秀
                    Abort(e);ntX中国设计秀
                    throw; ntX中国设计秀
                }ntX中国设计秀
                catch (System.Threading.ThreadAbortException e) { ntX中国设计秀
                    Abort(e); ntX中国设计秀
                    throw;ntX中国设计秀
                } ntX中国设计秀
                finally {ntX中国设计秀
                    SqlStatistics.StopTimer(statistics);ntX中国设计秀
                }ntX中国设计秀
            } ntX中国设计秀
            finally {ntX中国设计秀
                SqlDebugContext  sdc = _sdc; ntX中国设计秀
                _sdc = null; ntX中国设计秀
                Bid.ScopeLeave(ref hscp);ntX中国设计秀
                if (sdc != null) { ntX中国设计秀
                   sdc.Dispose();ntX中国设计秀
                }ntX中国设计秀
            }ntX中国设计秀
        } ntX中国设计秀
可以看到Close()方法并没有调用Dispose()方法,虽然有一行sdc.Dispose();,但是这只是释放SqlDebugContextntX中国设计秀
ntX中国设计秀
实例,和SqlConnection.Dispose()方法没有关系!ntX中国设计秀
ntX中国设计秀
那么区别在哪里呢?ntX中国设计秀
Close()方法只是关闭了连接,然后这个连接被存储到连接池,所以在调用Close()方法以后,还是可以再通过ntX中国设计秀
ntX中国设计秀
Open()方法来打开连接的ntX中国设计秀
而调用Dispose()方法以后,这个连接就不能在使用了!ntX中国设计秀
还有一个重要区别就是,当Close()方法并没有调用GC.SuppressFinalize(this);,这导致的直接后果就是在垃圾ntX中国设计秀
ntX中国设计秀
回收的时候需要进行终止化操作,这会导致这个实例的“代龄”提升,从而极大的延迟这个对象的回收时间!ntX中国设计秀
ntX中国设计秀
针对SqlConnection这个类来说,如果以后还需要使用这个连接可以使用Close()方法临时关闭连接,如果以后不需ntX中国设计秀
ntX中国设计秀
要使用这个连接了,可以优先选用Dispose()方法来释放资源,当然你可以使用using关键字来简化这个过程,ntX中国设计秀
ntX中国设计秀
OleDbConnection类和OdbcConnection类的源代码我没有找到,但是应该和SqlConnection类是类似的!ntX中国设计秀
ntX中国设计秀
让我们在看一个我们常用的类,看看FileStream类的Close()方法和Dispose()方法有什么区别:ntX中国设计秀
FileStream类的Close()方法是继承于Stream类的,源代码是这样的:ntX中国设计秀
        public virtual void Close() ntX中国设计秀
        {ntX中国设计秀
            Dispose(true); ntX中国设计秀
            GC.SuppressFinalize(this);ntX中国设计秀
        }ntX中国设计秀
FileStream类的Dispose()方法是继承于Stream类的,源代码是这样的:ntX中国设计秀
        public void Dispose() ntX中国设计秀
        {ntX中国设计秀
            Close(); ntX中国设计秀
        }ntX中国设计秀
是一个标准的Dispose模式的实现,

本文引用地址:/bc/article_46130.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明