0

我在我的应用程序中使用 TFDDataSet 组件时遇到问题。

如果客户有新订单,我有一个功能可以多次获取。如果它返回空函数结束。

...
 fdm_XMLREsumo.Close;
       fdm_XMLREsumo.Active := false;
       _DataSetJSON := SM.GetXMLResumoChave( pEnt_ID, pChave); //See Edit 1
       _DataSet.Close;
       _DataSet := TFDJSONDataSetsReader.GetListValueByName( _DataSetJSON, sXMLResumo );
       _DataSet.Open; <-- here's the problem 
       if not _DataSet.IsEmpty then begin
        exit;
       end;
       fdm_XMLREsumo.AppendData( _DataSet );
      ...

问题是每次执行_DataSet.Open;它都会在我的数据库中创建一个新连接。
因此,我有一个too many connections例外。

我检查了我的服务器属性,它是这样的:

许多线程处于睡眠状态

我已经尝试过,Connection.Close但没有任何效果。我读了这个,它解释说,即使你这样做,连接仍然存在,因为在内存中工作。还有这个人有类似的问题,但使用. 有谁知道我该如何解决这个问题?我正在使用MySQL_DataSet.Close_DataSet.Free_DataSet.Destroy
_DataSet.CloseDataSets
Query

编辑 1
正如@CraigYoung 帮助我说我的示例需要 MCVE

SM.GetXMLResumoChave方法:


在这里,它使用到在函数结束时关闭的数据库的连接。已经调试过,这里它不会在 MySQL 进程列表中留下一个打开的连接

function TDAOXMLResumo.GetXMLResumoChave(xEnt_id: Integer; xChave: String): TFDJSONDataSets;
begin
  if not oSM.FDConn.Connected then
    oSM.FDConn.Connected := true;
  QueryPesquisa.SQL.Clear;
  QueryPesquisa.SQL.Text :=
     ' select * from table' +
     ' where ent_id = :ent_id ' +
     '       and xre_chNFe = :xre_chNFe ';
  QueryPesquisa.ParamByName('ent_id').Asinteger   := xEnt_id;
  QueryPesquisa.ParamByName('xre_chNFe').Asstring := xChave;
  Result := TFDJSONDataSets.Create;
  //TFDJSONDataSetsWriter.ListAdd Opens the Query that is passed as parameter and store the data as JSON in the TFDJSONDataSets (Result) object
  TFDJSONDataSetsWriter.ListAdd(Result, sXMLResumo, QueryPesquisa);  
  //Closing the Query
  QueryPesquisa.Close;
  //Closing the Connection
  oSM.FDConn.Close;
end;`

基本上,_DataSet这里只接收一个 JSON List: _DataSet := TFDJSONDataSetsReader.GetListValueByName( _DataSetJSON, sXMLResumo );,然后打开它以访问其中的数据。

4

0 回答 0