How to detect connection lost ?

Forum related to the ZDBC API-Layer

Moderators: gto, cipto_kh, EgonHugeist

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

How to detect connection lost ?

Postby thiagodp » 13.10.2008, 22:34

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:

Postby designshouse » 27.02.2009, 14:56

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:

Postby seawolf » 28.02.2009, 10:23

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:

Postby designshouse » 01.03.2009, 14:56

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:

Postby seawolf » 01.03.2009, 15:25

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:

Postby designshouse » 01.03.2009, 17:27

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:

Postby seawolf » 01.03.2009, 23:28

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

Postby Babblo » 30.06.2010, 00:43

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

Postby kecap » 26.04.2013, 10:43

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

Postby fhaut » 24.05.2013, 17:28

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 ?


Return to “ZDBC (ZEOS Database Connectivity)”

Who is online

Users browsing this forum: No registered users and 0 guests