FirebirdSql.Data.FirebirdClient.FbDataAdapter的bug吗

发布时间 2023-12-15 13:28:34作者: 81

在连接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,使用后自己关闭要靠谱一点。