database - 如何使用ADT表重试Delphi TAdsConnection

标签 database delphi data-binding error-handling advantage-database-server

从中可以看到,我的成员(member)软件使用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

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

在这种情况下,是否存在错误捕获和重试连接的标准方法?还是在经过一定时间后才重复代码?

谢谢

最佳答案

您可以使用标准的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的处理。

关于database - 如何使用ADT表重试Delphi TAdsConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7146598/

相关文章:

大表上的 MySQL 性能问题 - 如何有效地缓存结果?

java - 表达式不能反转,因此不能用于双向绑定(bind)

c# - DateTime 应该存储在世界各地都可以访问的网站中的 UTC 时间还是(服务器的)本地时间

database - 数据库分析架构

delphi - 如何在 Delphi 中显示格式化(颜色、样式等)日志?

delphi - 是否可以使用 JclDebug 从引发异常的方法中获取参数值?

android - 无法获取简单的 TextView 以使用 Android 数据绑定(bind)

c# - 将 WPF DataGridComboBoxColumn 与 MVVM (WAF) 绑定(bind)

sql - 如何与具有复合主键的表建立关系?

delphi - 何时调用 TInterfacedObject.Destroy(ScopedLock 类)