TSQLServer::IsConnected?

Hey Folks,

The IsConnected function for TSQLServer, does it:

A) return true if you succesfully connected to the DB in the past and have yet to ask to disconnect

…or does it:

B) ping the DB in real time and check if the connection is good.

I suspect A is the answer. If I am correct, Is there a function provided to do B. If not, what is the fastest and least traffic heavy way to do B.

Cheers

Hi,

Answer on you question is - IsConnected returns true when connection was established in the past and was not closed. Even in the case of error in any SQL query execution IsConnected will return true until you close the connection.

There are many methods to test if connection is still there. The easiest way - execute simple SQL query and just look if execution was successful - you can use TSQLServer::Exec() method for that (try SQL command COMMIT or SHOW STATUS, for instance). Another possibility is to use TSQLServer::HasTable() method, which also sends request to the server each time when called. Of course, you should knew the name of existing table.

Regards, Sergey

Hello,

and thank you for your advice.

I already investigated such an option (make a simple query). The problem with this is that if the connection has been dropped, this operation takes a long time to return. What I need is something quick.

Is there some thing (I have looked around in the documentation quite a bit) which allows me specify a timeout for a query. Like, if there has been no return from the Query in 1 second dis-continue attempting to send the query.

Cheers

Andrew

Hi, Andrew

Up to now there is no possibility to specify such kind of timeouts in TSQLServer interface.

Do you have network problems or SQL server is hanging?
Which DB you are using?

I, probably, can provide a patch for case of MySQL

Sergey

Hey Sergey,

First of all, thank you so much for helping me out with this.

I am connecting to a MySQL DB.

The application I am developing I am just testing in my lab. It will be running on a machine which will be wirelessly connected to a private network which hosts the DB machine also.

Do you have network problems or SQL server is hanging?
It is likely that I will encounter network problems when the code is running in the real world scenario. Currently I am testing the codes behavior in this environment by plugging and unplugging the ethernet cable in/out of my machine here in the lab.

My current solution is to ping the server with the following function

bool DB_chat::DB_my_ping()
{

 bool connected = false;
 if(!(db_pointer == NULL || !db_pointer->IsConnected()))
    {
      string IP = string(db_pointer->GetHost());
      string command = "ping -c 1 -w 1 " + IP + " | grep -q \"1 received\"";
      if(system(command.c_str()) == 0)
        {
          connected = true;
        }
    }

 return connected;

}

but this is not optimal.

Cheers

Andrew

Hi, Andrew

Can you try to use another version of TMySQLServer.cxx, attached to this message. I add two new options - read_timeout and write_timeout, which you can specify in URL like this:

myslq://host.domain/dbname?read_timeout … timeout=10

This should set read and write timeouts to 10 seconds. If it helps for your case, I will submit changes to repository. But, seems to be, this will work only with the newest (6.0 alpha) version of mysql or on the Windows. Anyway, you can try.

Regards,
Sergey
TMySQLServer.cxx (23 KB)