How to test one connection is active?

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
nj_ww
Posts: 3
Joined: Wed 27 Jun 2012 12:37

How to test one connection is active?

Post by nj_ww » Sun 01 Jul 2012 12:50

My environment is :unidac 4.1.6,delphi xe2;win7 ,mysql5.1.
my question is when I open one uniconnection,if mysql server close this connectino because idel time is over wait_timeout. but how to test this connection is available?
the code like this:
1.create uniconnection and set property。
dbconn := TuniConnection.Create(nil);

dbconn.ProviderName := 'mysql';
dbconn.LoginPrompt := false;
dbconn.Server := 'localhost';
dbconn.port := 3306;
dbconn.Database := 'ftm_db';
dbconn.username := 'root';
dbconn.password := 'manager';
dbconn.SpecificOptions.Values['ConnectionTimeout:'] := '30';
dbconn.SpecificOptions.Values['charset'] := 'utf8';
dbconn.SpecificOptions.Values['Direct'] := 'true';
dbconn.Open;
2. code test connection active,
if temp.key.Connected then // here always true
temp.key.Close
else
temp.key.Open;

nj_ww
Posts: 3
Joined: Wed 27 Jun 2012 12:37

Re: How to test one connection is active?

Post by nj_ww » Wed 04 Jul 2012 03:32

who can help me!

AndreyZ

Re: How to test one connection is active?

Post by AndreyZ » Wed 04 Jul 2012 13:16

Hello,

If server breaks connection, client doesn't know anything about. That's why, to check if connection is broken or not, you should perform any operation through it. If connection is broken, the 'Connection lost' exception will be generated. To resume lost connection, you should use the TMyConnection.OnConnectionLost event handler. The OnConnectionLost event handler is used to process fatal errors and perform failover. To make the OnConnectionLost event handler work, you should set the TMyConnection.Options.LocalFailover property to True. Note that to use the OnConnectionLost event handler, you should add the MemData unit to the USES clause of your unit. Here is an example of using the OnConnectionLost event handler:

Code: Select all

procedure TMainForm.Button1Click(Sender: TObject);
begin
  MyConnection1.Options.LocalFailover := True;
  MyConnection1.Open;
end;

procedure TMainForm.MyConnection1ConnectionLost(Sender: TObject;
  Component: TComponent; ConnLostCause: TConnLostCause;
  var RetryMode: TRetryMode);
begin
  RetryMode := rmReconnectExecute;
end;
In this case, if connection was lost, MyDAC will try to reconnect and re-execute the abortive operation. For more information about the OnConnectionLost event handler, please read the MyDAC documentation.
Also, you can keep connection alive if you ping MySQL server time after time. To ping MySQL server, you can use the TMyConnection.Ping method that allows avoiding automatic disconnection of the client by the server in case of long time intervals between accessing the server.

nj_ww
Posts: 3
Joined: Wed 27 Jun 2012 12:37

Re: How to test one connection is active?

Post by nj_ww » Fri 06 Jul 2012 23:56

thank you 。
I will try it.

AndreyZ

Re: How to test one connection is active?

Post by AndreyZ » Mon 09 Jul 2012 12:26

If any other questions come up, please contact us.

Suhaimin
Posts: 79
Joined: Mon 06 May 2013 12:19

Re: How to test one connection is active?

Post by Suhaimin » Tue 11 Jun 2013 00:24

Hello,


I write code in formcreate :
uniconnection.close;
uniconnection.Options.LocalFailover := True;
uniconnection.connect;
uniquery.open;

UniconConnectionLost(Sender: TObject;Component: TComponent; ConnLostCause: TConnLostCause;var RetryMode: TRetryMode);
begin
RetryMode := rmReconnectExecute;
end;


but sometimes i get message connection timeout, so uniquery.open dont execute. . when i know connection already connect again? should i reopen uniquery ? thanks a lot

AndreyZ

Re: How to test one connection is active?

Post by AndreyZ » Tue 11 Jun 2013 07:44

The OnConnectionLost event occurs when the connection to the server was established and then was broken by some reason. Your situation is different, you cannot connect to the server. To avoid this problem, you should use the ConnectionTimeout specific option. The ConnectionTimeout property is used to specify the amount of time in seconds that can be expired before an attempt to make a connection is considered unsuccessful. Here is an example:

Code: Select all

UniConnection.SpecificOptions.Values['ConnectionTimeout'] := '60';
For more information, please refer to the "UniDAC and MySQL" section in the UniDAC documentation.

Suhaimin
Posts: 79
Joined: Mon 06 May 2013 12:19

Re: How to test one connection is active?

Post by Suhaimin » Tue 11 Jun 2013 13:26

Hello


thanks.. if i have a form with dbgrid + tuniquery +uniconnection.. while i am entering data via dbgrid suddenly my connection broken, what happened to dbgrid, is it still can do the entry data ? should i need to reopen the uniquery ? thanks a lot...

AndreyZ

Re: How to test one connection is active?

Post by AndreyZ » Tue 11 Jun 2013 14:03

In this case, you can use the OnConnectionLost event. If connection was lost and you set the RetryMode variable to rmReconnectExecute, UniDAC will try to reconnect and re-execute the abortive operation. If the attempt is successful, you can continue to work with data without any problems.

Suhaimin
Posts: 79
Joined: Mon 06 May 2013 12:19

Re: How to test one connection is active?

Post by Suhaimin » Tue 11 Jun 2013 23:48

Thanks a lot

AndreyZ

Re: How to test one connection is active?

Post by AndreyZ » Wed 12 Jun 2013 08:03

Feel free to contact us if you have any further questions about UniDAC.

Post Reply