Can't properly detect QSqlDatabase disconnect
-
Hello everyone, I have an issue trying to do a few things if an unexpected connection loss occurs, that is, a database disconnection.
My approach was using a method *isOpen()*on a QSqlDatabase object,
which is apparently unreliable for this scenario (it keeps returning true even when db connection is lost)Can anyone direct me on how to do this detection?
-
QSqlDatabase::isOpen() cannot possibly know if the intervening network is down permanently or if the remote server has shutdown/crashed because there is no mechanism by which it is told. When normal long network timeouts expire it may start to return false.
Run a known good query. If the query fails, probably with QSqlError::ConnectionError or QSqlError::UnknownError, then the connection is not usable.
-
@raulvc I tried calling transaction() and if the connection was timed out it return false in no time. This is how I know to close() the connection and open() it again. If transaction() returns true just call rollback(). I would like to see more opinions on that.