如何使用 ADT 表重试 Delphi TAdsConnection

发布于 2024-11-30 12:58:03 字数 1883 浏览 0 评论 0原文

据我所知,我的会员软件在 FormCreate 过程中使用以下代码连接到多个表:

 {Open the Sessions}
  Membership.LoginPrompt := False;
  Membership.Username := 'ONLINE';
  Membership.Password := '#######';
  Membership.ConnectPath := MembershipLocation;
  Membership.IsConnected := True;
  PosConnection.ConnectPath := PosLocation;
  PosConnection.IsConnected := True;
  Bookings.ConnectPath := BookingsLocation;
  Bookings.IsConnected := True;
  Local.ConnectPath := LocalLocation;
  Local.IsConnected := True;

  // Open all the tables
  for Wk1 := 0 to ComponentCount - 1 do
    begin
    {Skip the Tmp / New Tables}
    if ((Components[Wk1] is TAdsTable) and (TAdsTable(Components[Wk1]).Name = 'NewTable')) then
      Continue;
    if ((Components[Wk1] is TAdsTable) and (TAdsTable(Components[Wk1]).Name = 'TmpTable')) then
      Continue;
    {Is it a TTable}
    if Components[Wk1] is TAdsTable then
      TAdsTable(Components[Wk1]).Active := True;
    {Is it a TwwTable}
    if Components[Wk1] is TwwTable then
      TwwTable(Components[Wk1]).Active := True;
    {Is it a TQuery}
    if Components[Wk1] is TAdsQuery then
      TAdsQuery(Components[Wk1]).Active := True;
    end;


  {Activate the Membership Tables. This is due to passwords}
  Members.Active := True;
  MemTypes.Active := True;
  MembersById.Active := True;
  MemBookMSys.Active := True;

  {Rebuild the Secondry index on the MemBook table}
  if RebuildIdx = True then
    begin
    MemBook.Active := False;
    MemBook.Exclusive := True;
    MemBook.Active := True;
//  Check(DbiRegenIndexes(MemBook.Handle));
    MemBook.Active := False;
    MemBook.Exclusive := False;
    MemBook.Active := True;
    end;
  {Make the Table Active}
  MemBook.Active := True;

有时,当服务器尚未准备好时,连接会失败并且用户收到 Advantage 错误 7.xxx

我需要它重试连接多次,或者在经过一定时间后再次连接。

对于这种情况,是否有一种标准的错误捕获和重试连接方法?或者我应该在一段时间过去后重复该代码?

谢谢

From what I can see my Membership software connects to a number of tables using the following code in the FormCreate procedure:

 {Open the Sessions}
  Membership.LoginPrompt := False;
  Membership.Username := 'ONLINE';
  Membership.Password := '#######';
  Membership.ConnectPath := MembershipLocation;
  Membership.IsConnected := True;
  PosConnection.ConnectPath := PosLocation;
  PosConnection.IsConnected := True;
  Bookings.ConnectPath := BookingsLocation;
  Bookings.IsConnected := True;
  Local.ConnectPath := LocalLocation;
  Local.IsConnected := True;

  // Open all the tables
  for Wk1 := 0 to ComponentCount - 1 do
    begin
    {Skip the Tmp / New Tables}
    if ((Components[Wk1] is TAdsTable) and (TAdsTable(Components[Wk1]).Name = 'NewTable')) then
      Continue;
    if ((Components[Wk1] is TAdsTable) and (TAdsTable(Components[Wk1]).Name = 'TmpTable')) then
      Continue;
    {Is it a TTable}
    if Components[Wk1] is TAdsTable then
      TAdsTable(Components[Wk1]).Active := True;
    {Is it a TwwTable}
    if Components[Wk1] is TwwTable then
      TwwTable(Components[Wk1]).Active := True;
    {Is it a TQuery}
    if Components[Wk1] is TAdsQuery then
      TAdsQuery(Components[Wk1]).Active := True;
    end;


  {Activate the Membership Tables. This is due to passwords}
  Members.Active := True;
  MemTypes.Active := True;
  MembersById.Active := True;
  MemBookMSys.Active := True;

  {Rebuild the Secondry index on the MemBook table}
  if RebuildIdx = True then
    begin
    MemBook.Active := False;
    MemBook.Exclusive := True;
    MemBook.Active := True;
//  Check(DbiRegenIndexes(MemBook.Handle));
    MemBook.Active := False;
    MemBook.Exclusive := False;
    MemBook.Active := True;
    end;
  {Make the Table Active}
  MemBook.Active := True;

Occasionally when the server isn't ready yet the connection fails and the user gets an Advantage error 7.xxx

I need it to retry connection a number of times, or again after a certain amount of time has passed.

Is there a standard way of error catching and retrying connection for this scenario? Or should I simply repeat the code after a certain amount of time has passed?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪梦之恋 2024-12-07 12:58:03

您可以使用标准 try.. except 处理。

function TYourDataModule.ConnectToDatabases: Boolean;
begin
  Result := False;

  Membership.LoginPrompt := False;
  Membership.Username := 'ONLINE';
  Membership.Password := '#######';
  Membership.ConnectPath := MembershipLocation;
  PosConnection.ConnectPath := PosLocation;
  Bookings.ConnectPath := BookingsLocation;
  Local.ConnectPath := LocalLocation;

  // Try to make all the connections together. If any fail, we'll
  // hit the except block.
  try
    Membership.IsConnected := True;
    PosConnection.IsConnected := True;
    Bookings.IsConnected := True;
    Local.IsConnected := True;
  except
    on E: EAdsDatabaseError do
    begin
      // Make sure all connections are closed, in case
      // one or more succeeded before a failure. We'll
      // be set for next time.
      Membership.IsConnected := False;
      PostConnection.IsConnected := False;
      Booking.IsConnected := False;
      Local.IsConnected := False;
      Result := False;
    end;
  end;
end;

然后,您的调用代码可以在循环中使用该函数,直到它返回 true 或超过尝试次数:

var
  NumTrys: Integer;
const
  MAX_TRYS = 10;
  TRY_DELAY = 1000;
begin
  NumTrys := 0;
  while NumTrys < MAX_TRYS do
  begin
    if YourDataModule.ConnectToDatabases then
      Break;
    Inc(NumTrys);
    Sleep(TRY_DELAY);
  end;
  if NumTrys = MAX_TRYS then
    // Handle not being able to connect after all attempts.
end;

请注意,在 IDE 中运行时您将看到有关连接失败的异常消息,但在运行时则不会。如果您不想在 IDE 中看到它们,可以在“项目选项”对话框中关闭对 EADSDatabaseError 的处理。

You can use standard try..except handling.

function TYourDataModule.ConnectToDatabases: Boolean;
begin
  Result := False;

  Membership.LoginPrompt := False;
  Membership.Username := 'ONLINE';
  Membership.Password := '#######';
  Membership.ConnectPath := MembershipLocation;
  PosConnection.ConnectPath := PosLocation;
  Bookings.ConnectPath := BookingsLocation;
  Local.ConnectPath := LocalLocation;

  // Try to make all the connections together. If any fail, we'll
  // hit the except block.
  try
    Membership.IsConnected := True;
    PosConnection.IsConnected := True;
    Bookings.IsConnected := True;
    Local.IsConnected := True;
  except
    on E: EAdsDatabaseError do
    begin
      // Make sure all connections are closed, in case
      // one or more succeeded before a failure. We'll
      // be set for next time.
      Membership.IsConnected := False;
      PostConnection.IsConnected := False;
      Booking.IsConnected := False;
      Local.IsConnected := False;
      Result := False;
    end;
  end;
end;

Your calling code then can use the function in a loop until it returns true, or exceeds the number of attempts:

var
  NumTrys: Integer;
const
  MAX_TRYS = 10;
  TRY_DELAY = 1000;
begin
  NumTrys := 0;
  while NumTrys < MAX_TRYS do
  begin
    if YourDataModule.ConnectToDatabases then
      Break;
    Inc(NumTrys);
    Sleep(TRY_DELAY);
  end;
  if NumTrys = MAX_TRYS then
    // Handle not being able to connect after all attempts.
end;

Note that you'll see the exception message on connection failures when running in the IDE, but not at runtime. If you don't want to see them in the IDE, you can turn off handling of EADSDatabaseError in the Project Options dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文