[Lazarus] Is there a way to reconnect to postgresql if the connection fails?

Michael Van Canneyt michael at freepascal.org
Tue May 15 20:44:34 CEST 2018



On Tue, 15 May 2018, Luca Olivetti via Lazarus wrote:

> Hello,
>
> I'm trying to devise a strategy for when the connection to the server fails.
> I'm using zeos but I also tried with sqldb and I couldn't find a way.
> I open a connection to the server, then I stop the server and subsequent 
> operations (e.g. refreshing a query) fail (obviously since the 
> connection has been severed).
> However, for the same reason, I cannot close the connection (with sqldb 
> by setting PQConnection.connected:=false or with zeos by calling 
> ZConnection.Disconnect), since it raises an exception :-(
> With zeos I tried to get hold of the underlying handle and call PQReset 
> before closing the connection.
> The PQreset works, but the connection cannot be closed because it tries 
> to release prepared statements that don't exist in the new connection so 
> postgresql returns an error.....
>
> Is there a way to force the closing of a connection in this case so I 
> can try and reopen it again?

Try using forcedclose in SQLDb:

TheDatabase.Close(True);

Michael.


More information about the Lazarus mailing list