【发布时间】:2016-04-15 10:31:31
【问题描述】:
我使用向导创建了一个独立的 Datasnap TCP/IP 服务器。我选择了示例方法(回声串和反向串)。我保存了服务器并运行它。然后我创建了一个客户端应用程序,并使用 file-new-other,将 ClientModule 与 ClientClasses 单元一起添加到该客户端项目中。在主窗体上。我添加了一个按钮。在按钮的 onclick 事件处理程序上,我添加了以下代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
if ClientModule1.SQLConnection1.Connected then
begin
Button1.Text := 'Open';
ClientModule1.SQLConnection1.Close;
end
else
begin
Button1.Text := 'Close';
// ClientModule1.SQLConnection1.Open;
ClientModule1.ServerMethods1Client.ReverseString('myteststring');
end;
end;
这里的目的是模拟客户端定期登录和注销服务器而不是保持连接的情况。这对于部署到移动设备的应用尤其重要。
您可以看到我注释掉了 Connection.Open,因为第一次调用 ServerMethods1client 会打开连接。生成的代码如下所示:
function TClientModule1.GetServerMethods1Client: TServerMethods1Client;
begin
if FServerMethods1Client = nil then
begin
SQLConnection1.Open;
FServerMethods1Client := TServerMethods1Client.Create(SQLConnection1.DBXConnection, FInstanceOwner);
end;
Result := FServerMethods1Client;
end;
现在问题出现了。第一次单击按钮时,连接打开,并调用该方法。在第二次单击该按钮时,连接将关闭。 在第 3 次单击时,使用 TDBXCommand 代码引发异常“操作失败。连接已关闭”。
作为一种解决方法,我尝试了这个:
procedure TForm1.Button1Click(Sender: TObject);
begin
if ClientModule1.SQLConnection1.Connected then
begin
Button1.Text := 'Open';
ClientModule1.SQLConnection1.Close;
ClientModule1.ServerMethods1Client := nil;
end
else
begin
Button1.Text := 'Close';
// ClientModule1.SQLConnection1.Open;
ClientModule1.ServerMethods1Client.ReverseString('myteststring');
end;
end;
这确实解决了问题,因为 ClientModule1 的 FServerMethods1Client 实例已重置,因此创建代码再次运行,就像第一次运行时一样。
现在唯一的另一个问题是(我正在使用 Eurekalog)它会造成内存泄漏。
我做错了什么?在不重新启动应用程序的情况下重复连接/断开 Datasnap 服务器的正确方法是什么?
【问题讨论】:
标签: delphi datasnap delphi-10-seattle