在連接Firebird4數據庫時,使用以下:
FbDataAdapter da = new FbDataAdapter(sql, this.cnstring); DataTable dt = new DataTable(); da.Fill(dt); return dt;
在一直的相像中,FbDataAdapter在接收到連接字符串時,會自動創建一個Connection並Open使用,用完再Close,即不需要我們外部操作。但在實際中出現了錯誤,使用一會後會出現 Connection pool is full錯誤
應該是connection打開後沒有關閉,改爲以下代碼後不再出錯誤:
using (FbConnection cn = new FbConnection(this.cnstring)) { cn.Open(); FbDataAdapter da = new FbDataAdapter(sql, cn); DataTable dt = new DataTable(); da.Fill(dt); return dt; }
查詢FbDataAdapter的源代碼,增加一行代碼SelectCommand.Connection.Close()後程序正常
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (!_disposed)
{
_disposed = true;
if (_shouldDisposeSelectCommand)
{
if (SelectCommand != null)
{
SelectCommand.Connection.Close();
SelectCommand.Close();
SelectCommand.Dispose();
SelectCommand = null;
}
}
base.Dispose(disposing);
}
}
}
也許不是bug,也許是不同供應商實現的方式不一樣,因此以後無論是對mysql還是mssql時,都自己打開connection,使用後自己關閉要靠譜一點。