加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

database – 如何在整个应用程序中恢复/重试损坏的TADOConnectio

发布时间:2020-12-15 09:50:42 所属栏目:大数据 来源:网络整理
导读:我有一个带有全局TADOConnection的数据模块(默认的KeepConnection设置为true). 我现有的应用程序中有许多使用此全局TADOConnection的数据集和查询. 我想知道是否有一些智能方法可以在短暂的网络断开连接时恢复/重试ado连接? (这种情况有时发生在连接不稳定
我有一个带有全局TADOConnection的数据模块(默认的KeepConnection设置为true).
我现有的应用程序中有许多使用此全局TADOConnection的数据集和查询.

我想知道是否有一些智能方法可以在短暂的网络断开连接时恢复/重试ado连接? (这种情况有时发生在连接不稳定的客户端).

它很容易重现我需要的东西.只需在启动时打开TADOConnection即可.打开一些TADODataSet,然后禁用并启用“本地连接”.如果您尝试刷新数据集,则会引发EOleException异常

“Connection failure”

要么

“[DBNETLIB][ConnectionWrite (send()).]General network error. Check
your network documentation”

如果我重新启动应用程序一切都很好.

在网络断开连接时,TADOConnection不会触发任何事件.和TADOConnection.Connectedremains为true

当然我可以为每个TDataSet.Open或Execute使用try / catch,但我正在为我的大型应用程序寻找一些“集中式”解决方案.
所以在“连接失败”的情况下,我可以知道哪个数据集正在尝试打开,然后重试.

解决方法

在网络断开时永远不会开火.但是你可以在每个命令之前检查连接.因此,如果AdoConnection断开连接,您可以在此之后重新连接并执行命令.

如果你想集中解决方案并且你有1个连接,你可以这样做;

Const
  ConnectionTestString=' '; //Yes,it's work on Sql Server. If doesnt your db,you can use 'Select 1'

程序;

Procedure TDM.GetCommandResult_Conn(CText:String;Connection : TAdoConnection);
var Ado_Ds_Tmp:TAdoCommand;
Begin
    Ado_Ds_Tmp:=TAdoCommand.Create(self);
    try
      Ado_Ds_Tmp.Connection:=Connection;
      Ado_Ds_Tmp.ParamCheck := False;
      Ado_Ds_Tmp.CommandText:=CText;
      try
        Ado_Ds_Tmp.Execute;
      except
        DM.RaiseExceptionCreate('Error ! Command,('+StrToList(CText,' ')[0]+')');
      end;
    finally
      Ado_Ds_Tmp.Destroy;
    end;
end;

procedure TDM.ADOConnection1WillExecute(Connection: TADOConnection;
  var CommandText: WideString; var CursorType: TCursorType;
  var LockType: TADOLockType; var CommandType: TCommandType;
  var ExecuteOptions: TExecuteOptions; var EventStatus: TEventStatus;
  const Command: _Command; const Recordset: _Recordset);
var
   ErrorLogFileName : string;
   ErrorFile : TextFile;
   ErrorData : string;
   Msg : String;
begin
  try
    if (CommandText<>ConnectionTestString) then begin
      DM.GetCommandResult_Conn(ConnectionTestString,Connection);
    end;
  except
    try
      try
        Connection.Connected := False;
      except
      end;
      try
        Connection.ConnectionString := AdoConnectionString;
        Connection.Mode:=cmShareDenyNone;
      finally
        try
          Connection.Connected := True;
          // If you wanna log for frequency 
          ErrorLogFileName := ChangeFileExt(Application.ExeName,'.error.log');
          AssignFile(ErrorFile,ErrorLogFileName);
          if FileExists(ErrorLogFileName) then
            Append(ErrorFile)
          else
            Rewrite(ErrorFile);
          try
            ErrorData := Format('%s : %s : %s (%s / %s)',[DateTimeToStr(Now),'Disconnected but we reconnect.','','UserName : '+DBUser,'Client : '+GetComputerNetName]);
            WriteLn(ErrorFile,ErrorData);
          finally
            CloseFile(ErrorFile)
          end;
        except
          DM.RaiseExceptionCreate('ReConnection Failed!');
        end;
      end;
    except
    end;
  end;
end;

任何问题?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读