[Lazarus] Handling Firebird connection

SPRL AFOR aforsprl at gmail.com
Mon Aug 25 10:19:36 CEST 2014


Le 25/08/2014 09:32, Richard Mace a écrit :
> Hi all,
>
> I have the following code that I use to connect to a Firebird 
> database, which works fine, however, if the Firebird server becomes 
> unavailable, fIBConnection.Connected always returns true. What's the 
> best way of checking to make sure that the Firebird server is still 
> available before I attempt to connect to the database?
>
>
AFAIK the TIbConnection is based upon TSQLConnection itself based upon 
the TDatabase component. In all this chain the Connected property is 
defined in the latest component, the TDatabase. And the Connected 
property has a read function wich sends a boolean variable content 
(FConnected variable). That's why once the first connection has been 
established with the Firebird server (whether is succeeds or not), the 
connection status is kept thru all component life until next disconnect 
or re-connect operation takes place.
To solve the situation when the link to the databse server is broken I 
rely on the transaction mechanism. In fact I always control the 
transaction state allowing any query to be executed, as the Firebird 
server requires that any database access has to be done under the 
control of a transaction. This means thar when the 
MyTrans.StartTransaction fails I know (most of the times) that the link 
to the FB server is broken (omitting programmatic errors which can be 
controlled or checkced in another way). This does not protect your code 
from a connection exception between two consecutive database access (two 
consecutive calls to the fbclient library anyway). When I need a fine 
tuned control over a database access, after a initial MyDB.Connected := 
True, I always use the same sequence:

     try
         MyDatabase.Connedted := True;
     except
         on ...(exception process) do ... connection fault
     end
     [...] prepare data for the database
     try
         MyTransaction;StartTransaction
         try
             do whatever has to be done with the database
         finally
             MyTransaction.Commit
         end
     except
         do whatever needed when the exception rises: check whether its 
is an ordinary exception or a server link fault
         if MyTransaction.InTransaction then
             MyTransaction.RollBack;
     end;

This sequence does not protect against the server disconnection between 
two consecutive database calls. I easily can imagine that the same 
process occurs with any other database server hosted in a remote 
machine. Even when the database server is hosted in the same machine of 
the applicaton, TCP/IP communication applies.

Antonio.



---
Ce courrier électronique ne contient aucun virus ou logiciel malveillant parce que la protection avast! Antivirus est active.
http://www.avast.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20140825/a2c2e111/attachment-0003.html>


More information about the Lazarus mailing list