How to detect QSqlDatabase is still open???
Hi.
I have wrote some the thread run loop where i every iteration select some data from my database. In every iteration fisrt i check if database is open. if it open i am quering. Code looks like this.
Code:
void MyThread::run()
{
while (m_running)
{
// In this code if /etc/init.d/network stop (network closed) db.isOpen() returns true.
if (db.isOpen())
{
// some code
}
msleep(1000);
}
}
But, for testing i close the network, in linux using /etc/init.d/network stop. In this case db.isOpen() return true and in my query.exec() my program frozes in exec code.
How can i detect in this case that connection is stil open or network works or databse in remote server is not stopped?
Thanks.
Re: How to detect QSqlDatabase is still open???
Please use [code] tags around code.
What is the error type from QSqlDatabase::lastError() the first time you execute your query after the connection is lost?
Re: How to detect QSqlDatabase is still open???
db.isOpen() returns true even it db.open() is not called.
Re: How to detect QSqlDatabase is still open???
QSqlDatabase::database() will open the database when called unless Qt thinks it is already open, or you tell it not to with the optional second parameter.
Qt has no way of knowing the connection is lost due to some external event until you try to use the connection, hence my question: What is the error type from QSqlDatabase::lastError() the first time you execute your query after the connection is lost? I suspect you will receive a QSqlError of type QSqlError::ConnectionError and should close() the database at that point so that Qt knows it is no longer good. You should also check QSqlDatabase::isOpenError() if the server disappearing is a likely event.
Re: How to detect QSqlDatabase is still open???
There is not any output of QSqlDatabase::lastError(). QSqlDatabase::lastError() - outputs QSqlDatabasePrivate::database: unable to open database: "ORA-12157: TNS:internal network communication error when network is closed and in closing network i run my application. But when i run application, and after running i close network there is not any output and db.isOpen() returns true.
Added after 7 minutes:
Quote:
Originally Posted by
ChrisW67
QSqlDatabase::database() will open the database when called unless Qt thinks it is already open, or you tell it not to with the optional second parameter.
Qt has no way of knowing the connection is lost due to some external event until you try to
use the connection, hence my question: What is the error type from
QSqlDatabase::lastError() the first time you execute your query after the connection is lost? I suspect you will receive a
QSqlError of type QSqlError::ConnectionError and should close() the database at that point so that Qt knows it is no longer good. You should also check
QSqlDatabase::isOpenError() if the server disappearing is a likely event.
There is not any output of QSqlDatabase::lastError(). QSqlDatabase::lastError() - outputs QSqlDatabasePrivate::database: unable to open database: "ORA-12157: TNS:internal network communication error when network is closed and in closing network i run my application. But when i run application, and after running i close network there is not any output and db.isOpen() returns true.