PDA

View Full Version : How to detect QSqlDatabase is still open???



hashimov
25th January 2011, 07:20
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.


void MyThread::run()
{
while (m_running)
{
QSqlDatabase db = QSqlDatabase::database(CONNECTION_NAME);

// In this code if /etc/init.d/network stop (network closed) db.isOpen() returns true.
if (db.isOpen())
{
QSqlQuery query1(db);
// 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.

ChrisW67
25th January 2011, 07:26
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?

hashimov
25th January 2011, 07:58
db.isOpen() returns true even it db.open() is not called.

ChrisW67
25th January 2011, 08:09
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.

hashimov
25th January 2011, 08:17
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:


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.