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

In this forum we will discuss things relating the ZEOSLib 6.6.x stable versions

Moderators: gto, EgonHugeist

Post Reply
vfclists
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: 30.10.2007, 00:07

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

Post 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.
Wild_Pointer
Expert Boarder
Expert Boarder
Posts: 164
Joined: 18.03.2008, 13:03
Contact:

Post 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.
Post Reply