什么时候释放数据库连接
我研究了ms.net中的duwamish程序,发现其中数据库connection释放都是在dispose方法中,代码如下:
protected virtual void Dispose(bool disposing)
{
if (! disposing)
return; // we're being collected, so let the GC take care of this object
if (dsCommand != null )
{
if (dsCommand.SelectCommand != null)
{
if( dsCommand.SelectCommand.Connection != null)
dsCommand.SelectCommand.Connection.Dispose();
dsCommand.SelectCommand.Dispose();
}
dsCommand.Dispose();
dsCommand = null;
}
}
但我有个问题,如果说web开发中要讲究快速释放connection的话,我觉得最快的就是在每个方法中建立连接和释放连接,而不是在class的最后释放。而且既然有连接池,那么不停的连接也应该是可以的。
大家有什么意见呀