多次调用时 FireDac FDQuery 访问冲突

问题描述 投票:0回答:1

我有一个奇怪的错误,在我的函数中多次调用 FDQuery 时会引发访问冲突。基本上,我有一个函数,它循环遍历日期范围以生成从 SQL 表中提取的二维数据数组。

该函数调用另一个函数,该函数的工作是生成一个数组供一天使用。该函数是这样调用的:

while (date <= endDate) do
  begin
    try
        try
          FDQuery := TFDQuery.Create(nil);
          FDQuery.Connection := FDConnection;

          ConvertData(date, DataTableName, iMaxVarIndex, iSiteID, FDQuery, DataArray,
            sCommentsArray);
        finally
          if assigned(FDQuery) then FDQuery.Free;
        end;

      //for loop filling the 2d array...

      date := System.DateUtils.IncDay(date, 1);
      Inc(d);
    except
      on E: Exception do
      begin
        Result := false;
        break;
      end;
    end;
  end;

ConvertData 的实现是这样的:

procedure ConvertData(args);
var
//var decl
begin

  if not assigned(queryObj) then
    raise Exception.Create('Query object is unassigned.');

  with queryObj do
  begin
    // Prepare the SQL statement (obfuscated for demonstration)
    SQL.Text := 'SELECT col1, col2, col3, col4, col5, col6 FROM ' + tableName + ' WHERE dateColumn=:param1 AND siteColumn=:param2';
    ParamByName('param1').AsDate := dateParam;
    ParamByName('param2').AsInteger := siteID;

    try
      Open;
      if (Recordcount = 0) then exit;
    except
      on E: Exception do
      begin
        ShowMessage('Error executing query: ' + E.Message);
        Exit;
      end;
    end;
  end;

  try
    // Create and initialize memory streams
    commentsStream := TMemoryStream.Create;
    dataStream := TMemoryStream.Create;
    attrStream := TMemoryStream.Create;

    try
      // Copy blob data to memory streams (obfuscated for demonstration)
      commentsBlob := queryObj.CreateBlobStream(queryObj.FieldByName('col4'), bmRead);
      if assigned(commentsBlob) then
        commentsStream.CopyFrom(commentsBlob, queryObj.FieldByName('col1').AsInteger);

      dataBlob := queryObj.CreateBlobStream(queryObj.FieldByName('col5'), bmRead);
      if assigned(dataBlob) then
        dataStream.CopyFrom(dataBlob, queryObj.FieldByName('col2').AsInteger);

      attrBlob := queryObj.CreateBlobStream(queryObj.FieldByName('col6'), bmRead);
      if assigned(attrBlob) then
        attrStream.CopyFrom(attrBlob, queryObj.FieldByName('col3').AsInteger);

    // Convert streams to array

    with queryObj do
    begin
      Close;
    end;

    SetLength(attrArray, maxIndex);

    for counter1 := Low(attrArray) to High(attrArray) do
      attrArray[counter1] := NaN;

    ConvertStreamToData(attrStream, attrArray, tempNumericArray);

    SetLength(dataArray, maxIndex + maxIndex);
    SetLength(commentsArray, maxIndex + maxIndex);

    for counter1 := Low(dataArray) to High(dataArray) do
      dataArray[counter1] := NaN;

    for counter2 := Low(commentsArray) to High(commentsArray) do
      commentsArray[counter2] := '';

    ConvertCommentsStream(commentsStream, commentsArray, tempStringArray);
    ConvertDataStream(dataStream, dataArray, tempNumericArray);

    // Additional processing
    dateObj := SomeUtility.DecodeDateFunction(dateParam);

    for loopCounter := Low(attrArray) to High(attrArray) do
    begin
        dataArray[maxIndex + loopCounter] := attrArray[loopCounter];
    end;

    finally
      // Free the memory streams
      commentsStream.Free;
      dataStream.Free;
      attrStream.Free;
    end;
  except
    on E: Exception do
    begin
      ShowMessage('Error in ConvertData: ' + E.Message);
    end;
  end;
end;

访问冲突发生在我打开、关闭或释放 FDQuery 的行上,并且似乎总是在第二次调用该函数时发生。尽管在每次循环迭代中创建、打开、关闭和释放查询,它仍然会抛出这些异常,就好像对象无效一样。

可以在 here 找到与我 10 年前类似问题的帖子,尽管该解决方案不起作用或没有意义。

如有任何帮助,我们将不胜感激!

delphi memory-management pascal firedac
1个回答
0
投票

以下一些问题和建议:

  1. 放置 FDQuery := TFDQuery.Create(nil);尝试之前;
  2. 为什么要同时创建并分配fdconnection?
  3. Inc(d) 是什么意思?那个“d”是什么?
  4. 您是否在某处关闭了查询?
  5. 您的查询应该在此之前创建。还要考虑表演
© www.soinside.com 2019 - 2024. All rights reserved.