Page 1 of 1

How to detect connection lost ?

Posted: 13.10.2008, 22:34
by thiagodp
How can I detect connection lost, during normal program execution ?
i.e.: During my program execution I shutdown my database server by hand. Even implementing BeforeDisconnect and AfterDisconnect, I can't get the error. I think they are probably expecting normal disconnection, like when I call the Disconnect method.

Is there a way to detect connection lost without test it before each SQL command I run ? I don't want to add something like

Code: Select all

  if ( ! myDB.IsConnected ) throw DatabaseLostConnection();
before each myTable.Open() or myTable.ExecSQL().

Thanks in advance,

..:: Thiago ::..

Zeos 6.6.3-stable @ BCB 6

Posted: 27.02.2009, 14:56
by designshouse
i also need to solve this.
or is there posibility to catch this error and print my own error dialog
like "Database server error, please call your administrator"?

can somebody help?

Posted: 28.02.2009, 10:23
by seawolf
Hi, First of all what database are you using?
For Postgresql and MySQL it should be raised an exception,
for Firebird nothing has done

Posted: 01.03.2009, 14:56
by designshouse
I use MySQL. how can i raise and catch this exception?
i know how to handle exceptions for query but not for connection

and sorry for my english ;-)

Posted: 01.03.2009, 15:25
by seawolf
there's a method called ZConnection.PingServer to know if a MySQL DB server is connected, but I think it is already used to determine the connection status

Posted: 01.03.2009, 17:27
by designshouse
i know this metod but this is not what i looking for.
i can call ZConnection.PingServer before every query but Is there a way to detect connection lost without test it before each SQL command I run ?

Posted: 01.03.2009, 23:28
by seawolf
I think it is not possible. How can you do this without sending a message to the remote db server? Pinging the remote server waiting for a response?

Posted: 30.06.2010, 00:43
by Babblo
Take a look:

http://www.mysqlperformanceblog.com/201 ... d-harmful/

It's on a MySQL blog, but the idea is the same for any DB server.

Bye!

Posted: 26.04.2013, 10:43
by kecap
Hi i have made a change in ZDBCmysqlutils.pas thats always trying to reconnect if the connection is lost or the server has gone away.
you have to assign the zconnection you are using in app

procedure CheckMySQLError(PlainDriver: IZMySQLPlainDriver;
Handle: PZMySQLConnect; LogCategory: TZLoggingCategory; const LogMessage: string);
var
ErrorMessage: string;
ErrorCode: Integer;
begin
ErrorMessage := Trim(String(StrPas(PlainDriver.GetLastError(Handle))));
ErrorCode := PlainDriver.GetLastErrorCode(Handle);
if (ErrorCode <> 0) and (ErrorMessage <> '') then
begin
if SilentMySQLError > 0 then
raise EZMySQLSilentException.CreateFmt(SSQLError1, [ErrorMessage]);

DriverManager.LogError(LogCategory, PlainDriver.GetProtocol, LogMessage,
ErrorCode, ErrorMessage);
if (ErrorMessage='MySQL server has gone away') or (copy(ErrorMessage,7,7)='connect') then try
Komponenty.ZConnection1.Reconnect;
finally
ErrorMessage:='database connection lost'
end;

raise EZSQLException.CreateWithCode(ErrorCode,
Format(SSQLError1, [ErrorMessage]));
end;
end;

Posted: 24.05.2013, 17:28
by fhaut
hi,

in Zeos 7.1.0-alfa, using postgresql 8.4 this issue remains, someone have a way to detect the connection lost ? this happen on wireless network where is poor signal.

Exemple:
Create a project
Add TZConnection, TZQuery, TDataset, DBGrid
Add TButton with code to connect to postgresql
Restart the postgresql service or reboot wireless access point
try do add a record on dbgrid.


any idea ?