How to detect connection lost ?

Forum related to the ZDBC API-Layer

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
thiagodp
Fresh Boarder
Fresh Boarder
Posts: 1
Joined: 19.04.2008, 05:33

How to detect connection lost ?

Post 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
designshouse
Fresh Boarder
Fresh Boarder
Posts: 20
Joined: 21.11.2005, 10:13
Location: Pieštany
Contact:

Post 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?
seawolf
Zeos Dev Team *
Zeos Dev Team *
Posts: 385
Joined: 04.06.2008, 19:50
Contact:

Post 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
designshouse
Fresh Boarder
Fresh Boarder
Posts: 20
Joined: 21.11.2005, 10:13
Location: Pieštany
Contact:

Post 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 ;-)
seawolf
Zeos Dev Team *
Zeos Dev Team *
Posts: 385
Joined: 04.06.2008, 19:50
Contact:

Post 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
designshouse
Fresh Boarder
Fresh Boarder
Posts: 20
Joined: 21.11.2005, 10:13
Location: Pieštany
Contact:

Post 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 ?
seawolf
Zeos Dev Team *
Zeos Dev Team *
Posts: 385
Joined: 04.06.2008, 19:50
Contact:

Post 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?
Babblo
Fresh Boarder
Fresh Boarder
Posts: 4
Joined: 07.06.2009, 23:28

Post 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!
kecap
Fresh Boarder
Fresh Boarder
Posts: 7
Joined: 05.05.2012, 14:27
Location: slovakia

Post 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;
fhaut
Junior Boarder
Junior Boarder
Posts: 29
Joined: 10.04.2009, 15:55

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