首页 > 解决方案 > Datasnap 获取 10054 - 套接字错误 #10054 连接被对等方重置。在 TDBXCommand.Prepare 中

问题描述

我有一个稳定的 Delphi 10.2.3 TCP/IP Datasnap 服务器和客户端,可以在 99.9% 的时间里完美运行。有时,用户会收到“10054 Connection reset by peer”错误。我使用 Eurekalog,调用堆栈报告显示错误发生在DBXCommand.Prepare生成的语句中ClientClassesUnit

function TServerMethods3Client.UpdateTask(ID: Integer): Boolean;
begin
  if FUpdateTaskCommand = nil then
  begin
    FUpdateTaskCommand := FDBXConnection.CreateCommand;
    FUpdateTaskCommand.CommandType := TDBXCommandTypes.DSServerMethod;
    FUpdateTaskCommand.Text := 'TServerMethods3.UpdateTask';
    FUpdateTaskCommand.Prepare; // --> exception is raised here
  end;
  FUpdateTaskCommand.Parameters[0].Value.SetInt32(ID);

当然,与 Datasnap 服务器的连接是在 calll to server 方法之前实现的,使用通常的方法:

SQLConnection1.Connected := True
Server := TServerMethods3Client.Create(SQLConnection1.DBXConnection);
try
  Result := Server.UpdateTask(CDSTask.FieldByName('ID').AsInteger)

问题不在于连接到服务器失败,而是连接没有“保持正常”,甚至几毫秒都没有。

任何意见表示赞赏。

标签: delphitcpdatasnap

解决方案


尽管我真的不想这样做,但我最终这样做了:

function ClientModule1.UpdateTask: Boolean;
var
  i: integer
begin
  Result := False;
  i := 0;
  while (i < 3) and (not Result) do 
  begin
    if ServerConnect then
    begin
      Server := TServerMethods3Client.Create(SQLConnection1.DBXConnection);
      try
        try
          Result := Server.UpdateTask(CDSTask.FieldByName('ID').AsInteger);
        except
        on e: exception do
        begin
          inc(i)
          if i = 3 then
            MessageDlg(e.Message, mtError, [mbOk], 0);
          Sleep(1000);
        end
        end
      finally
        Server.Free;
        SQLConnection1.Connected := False;
      end;
    end;
  end;
end;

function ClientModule1.ServerConnect: Boolean;
var
  i: Integer;
begin
  if SQLConnection1.Connected then
    SQLConnection1.Close;
  i := 0;
  while (not SQLConnection1.Connected) and (i < 3) do
  begin
    try
      SQLConnection1.Open;
    except
    on e: exception do
    begin
      inc(i)
      if i = 3 then
        MessageDlg(e.Message, mtError, [mbOk], 0);
      sleep(1000);
    end;
    end;
  end;
end;

推荐阅读