I have a program that is on a wifi connection and that sometimes loses the connection.
I want to do a test if I have a connection to server.
I'm using MyQuery with Cached Updates and using mysql direct.
If I use MyConnection.Ping () to test/reconnect to the server and do not have any connection to the server all my updated data is gone.
To recreate the problem:
Code: Select all
MyConnection->Connect ();
MyQuery->CachedUpdates = true;
MyQuery->Sql->text = "Select * Table1 limit 10";
MyQuery->Active = true;
MyQuery->FieldByName("Field1")->AsString = "SampleData";
// Now down the server and test ping().
try{
  MyConnection->Ping ();   // Connection is closed and MyQuey->Active is set to false
}
catch(...){ // Connection lost and MyQuery not active }What I want to do is to have a function that can check if I have a connection to the server and then creates/reconnects to the server.
If I do not have a connection to the server MyConnection uses the existing open connection to the server this keeps the data in the MyQuery.
My Existing Sample:
Code: Select all
//---------------------------------------------------------------------------
class PACKAGE TMyConnectionEx : public TMyConnection
{
private:
protected:
public:
	__fastcall TMyConnectionEx(TComponent* Owner);
   
   
   int	__fastcall IsConnectionOK(void)
   {
	 
	 TMyConnection *ConTest = NULL;
	 int Ok;
	 try{
		 if(Connected != true)
		 {
			 Ping();
			 return !Connected;
		 }
		 if(Ok = IConnection()->MySQLAPI->mysql_ping(IConnection()->MySQL))
		 {
 		     try
		     {
					   ConTest = new TMyConnection(this);
					   ConTest->Assign(this);
					   ConTest->Connect();
					   if(ConTest->Connected == true)
					   {
						   if(ConTest) delete ConTest;
						   try          { Ping();  }
						   catch(...)   { Ok = !Connected; }
						   return Ok;
					   }
				  }
				  catch(...)
				  {
						  if(ConTest)   delete ConTest;
						  return Ok;
				  }
			 }
			 return Ok;
	 }
	 catch(...)
	 {
	   return  1;
	 }
   }
__published:
};