July 12, 2012

robvr robvr
Lab Rat
4 posts

[SOLVED] QSqlDatabase annoying warnings

 

I have the following bit of code:

  1. class zzzz
  2. {
  3. ...
  4.     QSqlDatabase m_database;
  5. }

and later

  1. zzzz::function()
  2. {
  3.    m_database= QSqlDatabase::addDatabase("QSQLITE");
  4. ...
  5. }

Whenever this runs, I get hit with the following lines on my console:

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

5 replies

July 12, 2012

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

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.

July 12, 2012

robvr robvr
Lab Rat
4 posts

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

July 12, 2012

franku franku
Hobby Entomologist
132 posts

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.

 Signature 

Keep in mind: This, Jen, is the internet.
.. frank

July 12, 2012

ChrisW67 ChrisW67
Robot Herder
371 posts

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:

  1. // make sure any query or model depending on the database connection is
  2. // out-of-scope/deleted, then
  3.  
  4. // For a named connection
  5. QSqlDatabase::removeDatabase("FunkyDBName");
  6.  
  7. // For the default connection
  8. QString connName;
  9. {
  10.   QSqlDatabase db = QSqlDatabase::database();
  11.   connName = db.connectionName();
  12. }
  13. 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().

July 13, 2012

robvr robvr
Lab Rat
4 posts

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

 
  ‹‹ [SOLVED]Function executing an action that is not there (???)      QTcpSocket accepts localhost but not 127.0.0.1 ››

You must log in to post a reply. Not a member yet? Register here!