Results 1 to 6 of 6

Thread: Properly close database handlers

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Properly close database handlers

    Hi all!

    I have a database application running in a remote server that therefore works as dataserver. The client send a request and the server replies to it after getting some info from the database. The code works but must be sharpen because every 20 connections( more or less) it fails to connect to the database. Is this message related to the problem? I get it every connection but it still works:
    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.
    how should I close the database properly?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Properly close database handlers

    Before you remove a connection make sure all QSqlQuerry/QSqlDatabase/QSql*** object's are destroyed of cleared. Then you can remove the database.

  3. #3
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Properly close database handlers

    Quote Originally Posted by Lykurg View Post
    Before you remove a connection make sure all QSqlQuerry/QSqlDatabase/QSql*** object's are destroyed of cleared. Then you can remove the database.
    Thanks for your answer!

    I already tryed to clear close and delete all objects but the message keeps showing. I will post some of the code:

    Header:

    Qt Code:
    To copy to clipboard, switch view to plain text mode 

    Constructor:
    Qt Code:
    1. db = QSqlDatabase::addDatabase("QMYSQL"); //select database type
    2. db.setHostName("localhost"); //tell where the database is
    3. db.setDatabaseName("qt"); //Database name
    4. db.setUserName("user"); //username to connect to the database
    5. db.setPassword("password"); //password from user
    To copy to clipboard, switch view to plain text mode 

    Function LoadProduct:
    Qt Code:
    1. //check for errors on database connection
    2. if (!db.open()) {
    3. qDebug() << db.lastError();
    4. // return 4;
    5. }
    6. //set table to use
    7. QString table = "mytable";
    8. QSqlQuery query("USE " + table);
    9.  
    10. //Query to get table info
    11. query.exec("Select ProductName from "+ table);
    12.  
    13. //routine to read values
    14. while (query.next()) {
    15.  
    16. QSqlRecord record = query.record();
    17.  
    18. //get ProductNames
    19. ProductNames << query.value(record.indexOf("ProductName")).toString();
    20. numberOfProducts++;
    21. }
    To copy to clipboard, switch view to plain text mode 

    How should I close it now? I already tryed, db.close(), query.clear, db.removeDatabase("QMYSQL"), delete db, delete query, QSqlDatabase::removeDatabase("QMYSQL"), etc...
    Remind you that the application works most of the times but the message keeps on comming.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Properly close database handlers

    Simple solution: Don't use a member variable for QSqlDatabase! There is no need for it. You can always use the static QSqlDatabase::database() function to get a handle when you need it.

  5. #5
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Properly close database handlers

    do you mean that I don't need the db variable?
    because I have the QSqlDatabase::database() but there is no function to set the username, password, database name and host..

    or am I confused?

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Properly close database handlers

    You need it, but not as a member variable:
    Qt Code:
    1. //somewhere in your definition
    2. void Foo::bar()
    3. {
    4. //...
    5. {
    6. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //select database type
    7. db.setHostName("localhost"); //tell where the database is
    8. db.setDatabaseName("qt"); //Database name
    9. db.setUserName("user"); //username to connect to the database
    10. db.setPassword("password"); //password from user
    11. } // here the temporary "db" gets deleted.
    12. //...
    13. }
    14.  
    15. void Foo::baz()
    16. {
    17. QSqlQuery q(QSqlDatabase::database());
    18. // use q
    19. } // here q gets deleted...
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Lykurg for this useful post:

    ruben.rodrigues (23rd September 2010)

Similar Threads

  1. How to properly install QCA on Mac OS?
    By unix7777 in forum Newbie
    Replies: 1
    Last Post: 2nd September 2010, 00:20
  2. Mapping menu actions to handlers
    By xtal256 in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2010, 07:41
  3. Replies: 1
    Last Post: 22nd March 2010, 14:38
  4. How to install Qxt properly?
    By blurboy in forum Newbie
    Replies: 3
    Last Post: 18th October 2009, 20:12
  5. How do I use QTcpSocket properly ?
    By mnemonic_fx in forum Qt Programming
    Replies: 13
    Last Post: 29th March 2007, 20:33

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.