Results 1 to 4 of 4

Thread: Close and reopen connection to database

  1. #1
    Join Date
    Sep 2021
    Posts
    6
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Close and reopen connection to database

    I have a 'small but long' task. On a user request, the query to database should happen, this query can run for some noticeable time.
    So this task is moved to a thread with a run() like this:

    Qt Code:
    1. void SubTask::run() {
    2. const QString connName = QString::number((quintptr)QThread::currentThreadId());
    3. QSqlDatabase db2 = QSqlDatabase::cloneDatabase(db, connName); // db is SQLite database created in the main thread
    4. if (!db2.open()) {
    5. err = "Failed to open db connection" + connName;
    6. qCritical() << err;
    7. }
    8. QSqlQuery qry(db2);
    9. qry.exec( .... );
    10. qry,clear();
    11. db2.close();
    12. QSqlDatabase::removeDatabase(connName);
    13. }
    To copy to clipboard, switch view to plain text mode 

    It works fine, query is executing each time user requests it.
    But in the console I have a warning:
    Qt Code:
    1. QSqlDatabasePrivate::removeDatabase: connection '140551504512768' is still in use, all queries will cease to work.
    To copy to clipboard, switch view to plain text mode 
    One such warning for each execution.

    What am I doing wrong? How the connection should be closed correctly?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Close and reopen connection to database

    What am I doing wrong? How the connection should be closed correctly?
    You may not need the call to QSqlDatabase::removeDatabase(). The clone (db2) will go out of scope when run() exits, thus closing the connection. See the examples and discussion under removeDatabase(). However, if it turns out you do need it, then you can put lines 3 - 11 inside of their own scope ("{}") and avoid the warning.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    White_Owl (9th October 2021)

  4. #3
    Join Date
    Sep 2021
    Posts
    6
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Close and reopen connection to database

    Quote Originally Posted by d_stranz View Post
    You may not need the call to QSqlDatabase::removeDatabase(). The clone (db2) will go out of scope when run() exits, thus closing the connection.
    Actually - no.
    I am not sure about the mechanics of the process, but if the name of the secondary connection is a constant, then there are warnings about "such connection already exists, closing old one". Even though that connection belonged to a thread which was ended hours ago.
    That was the reason why the connection name was renamed from a constant to a thread number. It masked the problem of existing connection and produced a leak. Which we were looking for for years...

    Quote Originally Posted by d_stranz View Post
    See the examples and discussion under removeDatabase(). However, if it turns out you do need it, then you can put lines 3 - 11 inside of their own scope ("{}") and avoid the warning.
    Strangely, but yes, that actually solved all the problems. Adding another layer of visibility - did a trick of completely disposing of a secondary connection.

    So the actual answer to main question of this topic:
    The db2 object should be destroyed before calling QSqlDatabase::removeDatabase(). The destruction can either be by manually calling the destructor or by the object going out of visibility scope and calling the destructor that way.

  5. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Close and reopen connection to database

    Quote Originally Posted by White_Owl View Post
    The db2 object should be destroyed before calling QSqlDatabase::removeDatabase(). The destruction can either be by manually calling the destructor or by the object going out of visibility scope and calling the destructor that way.
    Correct, properly documented in the QSqlDatabase::removeDatabase() documentation

Similar Threads

  1. QSqlDatabase Connection Close on Destruction
    By Sanuden in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2011, 16:32
  2. QHttp - how close TCP connection
    By juzwa in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2011, 20:23
  3. How to correctly close a database connection
    By anoraxis in forum Qt Programming
    Replies: 3
    Last Post: 8th April 2011, 17:28
  4. database connection
    By mak_user in forum Newbie
    Replies: 1
    Last Post: 9th February 2011, 10:23
  5. Connection With database
    By sudheer168 in forum Qt Programming
    Replies: 4
    Last Post: 22nd December 2010, 10:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.