[SOLVED] QSqlDatabase annoying warnings
-
wrote on 12 Jul 2012, 15:01 last edited by
I have the following bit of code:
@
class zzzz
{
...
QSqlDatabase m_database;
}
@and later
@
zzzz::function()
{
m_database= QSqlDatabase::addDatabase("QSQLITE");
...
}
@Whenever this runs, I get hit with the following lines on my console:
bq. QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.Is there any way to get rid of these complaints? Or better yet, can someone tell me how to do this correctly? I'm obviously missing something here..
Thanks
Rob
-
wrote on 12 Jul 2012, 18:20 last edited by
It's quite hard to say what you acutally did wrong without seeing your code, but you will have to make sure that all queries have been gone out of scope before the database does or is explicitly removed.
-
wrote on 12 Jul 2012, 19:39 last edited by
Oh dear, looks like I messed up...
I can't very well post the actual code, since that belongs to a customer. I thought the stripped out bits I did post were enough to reproduce the issue, but the quick and dirty version I just put together at home does not show the problem. I'll start stripping down the real code until the bare minimum to show the problem remains when I get back to the office tomorrow. I'll post further updates when I have a complete minimal example.
Rob
-
wrote on 12 Jul 2012, 20:35 last edited by
I've got some applications where closing the database gives warnings like the one mentioned first. But it newer harmed anyway. What I do not understand is the second message. It looks like you were opening the database connection more than once.
-
wrote on 12 Jul 2012, 21:46 last edited by
If you call function() more than once without cleanly calling QSqlDatabase::removeDatabase() in between then you will get this warning. To cleanly close the database:
@
// make sure any query or model depending on the database connection is
// out-of-scope/deleted, then// For a named connection
QSqlDatabase::removeDatabase("FunkyDBName");// For the default connection
QString connName;
{
QSqlDatabase db = QSqlDatabase::database();
connName = db.connectionName();
}
QSqlDatabase::removeDatabase(connName);
@You will have difficulty because you are holding m_database as a member variable and it cannot be out-of-scope. I find it easier to fetch a local handle handle using QSqlDatabase::database() in routines that need it and never holding a handle for a long period.
On the other hand, you may be calling QSqlDatabase::addDatabase() when you really want QSqlDatabase::database() to get a handle to an existing database for use inside function().
-
wrote on 13 Jul 2012, 09:24 last edited by
Thank you all for the responses, and just as importantly, for providing a forum where asking for help forced me to to take a new, hard look at all the code to be able to explain the problem.
It turns out somewhere else in the code there was also a call to QSqlDatabase::addDatabase("QSQLITE"), which had escaped my notice the first times round. This second call should not have been there in the first place, and eliminating it solved this issue. In retrospect, the runtime system was entirely correct in complaining as it did (no surprise, really..).Rob
1/6