How to test one connection is active?
How to test one connection is active?
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;
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;
Re: How to test one connection is active?
who can help me!
-
AndreyZ
Re: How to test one connection is active?
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: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.
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;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.
Re: How to test one connection is active?
thank you 。
I will try it.
I will try it.
-
AndreyZ
Re: How to test one connection is active?
If any other questions come up, please contact us.
Re: How to test one connection is active?
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
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?
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:For more information, please refer to the "UniDAC and MySQL" section in the UniDAC documentation.
Code: Select all
UniConnection.SpecificOptions.Values['ConnectionTimeout'] := '60';Re: How to test one connection is active?
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...
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?
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.
Re: How to test one connection is active?
Thanks a lot
-
AndreyZ
Re: How to test one connection is active?
Feel free to contact us if you have any further questions about UniDAC.