Question: 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.

Answer: 
There is a bug related with QSqlDatabase::isOpen() in Qt. 

<https://bugreports.qt.io/browse/QTBUG-223>
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.