views:

84

answers:

4

When I closed MySql server, how can I understand that mysql server is gone away from my Qt program?

Edit:

Here my trial:

When I close MySql, I get these results, and I can't catch that MySql is closed.

My Code Snippet is

QSqlQuery query(db);
query.exec("SELECT * From RequestIds");
qDebug()<<query.lastError();
qDebug()<<db.lastError()<<QTime::currentTime();
qDebug()<<db.isOpen();
qDebug()<<db.isValid();

and output is:

QSqlError(2006, "QMYSQL: Unable to execute query", "MySQL server has gone away") 
QSqlError(-1, "", "") QTime("14:22:58") 
true 
true

I don't understand why db.isOpen() returns true.

+1  A: 

Your program has no idea of its surroundings. If something changes, you may be able to have the OS notify your program, or you'll have to test yourself.

If the database connection closes before your program, the status from the connection should return some kind of error code. You are checking status from the connection functions?

Write a simple program that opens a window and upon the click of a button, writes to the database. After writing to the database, the program should display the status in the window. Run your program. Press button to get the "controlled" response. Close the database then click on the button again.

You may be able to do this with a debugger, depending on the ability of the debugger & OS to queue up messages.

Thomas Matthews
+1  A: 

QSqlQuery::lastError() should give you an error if your query via QSqlQuery::exec() has failed. Also QSqlDatabase::isOpen() should report the state of your connection, QSqlDatabase::lastError() is also available

Harald Scheirich
+1  A: 

You can use isOpenError to determine whether opening the initial database connection was successfull. I agree that isOpen returning true is confusing.

To monitor the database connection I repeatedly try to open and close a lightweight MySQL connection (e.g. every 3 seconds):

    #include <mysql/mysql.h>

    mysql_init(&connection);
    MYSQL *result = mysql_real_connect(&connection,
            host.isNull() ? static_cast<const char *>(0) : host.toLocal8Bit().constData(),
            user.isNull() ? static_cast<const char *>(0) : user.toLocal8Bit().constData(),
            pass.isNull() ? static_cast<const char *>(0) : pass.toLocal8Bit().constData(),
            dbName.isNull() ? static_cast<const char *>(0) : dbName.toLocal8Bit().constData(),
            0,
            0,
            0);

    bool currentlyConnected = (result != 0);

In the above example, host, user, pass, and dbName are QString instances containing the connection information. Note that you need the MySQL development headers.

Ton van den Heuvel
A: 

Please check your MySQL sysem variables.

interactive_timeout

wait_timeout

By default, they are set to 28800 (8 hours). If you have these variables set in /etc/my.cnf, then make sure your app does not hold the connect pass that limit. Otherwise, you may want to have interactive_timeout and wait_timeout set to a high value.

Even though you are not using mysql_pconnect, make sure PHP is not allowing persistent connections. Check other settings to see if one of two things happen:

1) apache thinks the connection is gone while MySQL still has the connection open.

2) apache thinks the connection is open while MySQL closed the connection already.

Something else to keep in mind : run this command in Linux

netstat | grep 3306 | grep TIME_WAIT

If this number exceeds the actual number of DB connections the mysqld processs has, you may have issues with your kernel.

Have fun looking !!!

http://www.linkedin.com/in/rolandoedwards

www . logicworks . net

RolandoEdwards