Page 1 of 1

How to test if a TConnection is active before reading data.

Posted: 12.07.2010, 12:23
by vfclists
Is there a way of testing when a TZConnection is active when the network is down?

The TZConnection.Connected property is true even when the network connection is down, and it doesn't seem to change when an attempt to connect to the database fails.

Isn't there a way of monitoring the TZConnection rather than trying to open a table and realizing that the connection is down?

Is there also a way of trapping the exception when you try to open the table when there is no connection? I try testing for E:EZSQLException but it doesn't seem to catch everything.

Posted: 12.07.2010, 12:58
by Wild_Pointer
vfclists,
you can TZConnection.Ping to see if the connection is down, but checking it every time before executing query is not efficient. In stead you could try sometring like that:

Code: Select all

procedure OpenQuery(AQuery: TZQuery; ARetries:Integer);
var _RetriesLeft: Integer;
begin
  _RetriesLeft = ARetries;
  While (_RetriesLeft  > 0) and not AQuery.Active do
  try
    AQuery.Open;
  except
    On E:Exception do
    begin
       if _RetriesLeft = 0 then
         Raise;
      _RetriesLeft  := _RetriesLeft - 1;
      If _Connection_lost_ then
      try
        AQuery.Connection.Reconnect;
      except
      end;
    end
  end;
end;
(Code not checked or compiled)
_Connection_lost_ -means some expression finding out if connection is lost. You can use E.Message for that, just see what error messages you get when connection is lost.