Results 1 to 2 of 2

Thread: Removing database connections

  1. #1
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows

    Default Removing database connections

    I ran into an interesting problem today, and although I seem to have fixed it, I'm not totally sure that my fix was a good one.

    First, some background. My program is a multi-threaded server that requires database access from each each thread. Because QSqlDatabase objects can't use the same connection from different threads (per the docs), I generate a new connection (with a unique name) in each thread. Since I don't want to have a prgoram with a million unused connections, I was deleting the connection when the thread ended. This was where my problems started. Deleting the connection resulted in a "connection still in use warning"

    Here is what my original code looked like:

    Qt Code:
    1. class configDb
    2. class configDb
    3. {
    4. public:
    5. configDb();
    6. ~configDb();
    7. private:
    8. QSqlDatabase configDb_;
    9. };
    10.  
    11. void configDb::configDb()
    12. {
    13. QString connectionName = "connection"+QString::number(rand());
    14. configDb_ = QSqlDatabase::addDatabase("QSQLITE",connectionName_);
    15. configDb_.setDatabaseName(QCoreApplication::applicationDirPath() + "/PictoServer.config");
    16. configDb_.open();
    17. }
    18.  
    19. void configDb::~configDb()
    20. {
    21. configDb_.close();
    22.  
    23. //WARNING occurs here:
    24. QSqlDatabase::removeDatabase(configDb_.connectionName());
    25. }
    To copy to clipboard, switch view to plain text mode 

    My first assumption was that I had somehow left an open query, but that wasn't possible since the queries were all in separate functions and were defined locally (and hence were deleted before the destructor was called).

    After banging my head against the wall for a bit, I realized that I can't call removeDatabase while the configDb_ object still exists. Since it's a class member, I can't figure out an easy way to destruct it (delete doesn't work on a non-pointer, and I can't wait to call the class destructor until after the class itself is deleted).

    So, to fix all of this, I am using pointers (since you can call delete on a pointer):

    Qt Code:
    1. class configDb
    2. {
    3. public:
    4. configDb();
    5. ~configDb();
    6. private:
    7. QSqlDatabase *configDb_;
    8. QString connectionName_;
    9. };
    10.  
    11. void configDb::configDb()
    12. {
    13. connectionName_ = "connection"+QString::number(rand());
    14. configDb_ = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE",connectionName_));
    15. configDb_->setDatabaseName(QCoreApplication::applicationDirPath() + "/PictoServer.config");
    16. configDb_->open();
    17. }
    18.  
    19. void configDb::~configDb()
    20. {
    21. configDb_->close();
    22. delete configDb_;
    23.  
    24. QSqlDatabase::removeDatabase(connectionName_);
    25. }
    To copy to clipboard, switch view to plain text mode 

    The above code fixes my problem, but it seems like a total hack. This makes me worried that I have misunderstood something really fundamental, either about QSqlDatabase, or (even more embarassing) the destruction of objects in C++.

    I suspect that someone will suggest simply generating an infinite number of connection (by never removing them), or using a single connection. The reason these approaches won't work is because I am writing a server. Since it is likely to be running for an extended period of time (assuming I've done my job), generating an infinite number of connections will result in a problem somewhere down the line. At the same time, since it is a server, it makes sense to be constantly spinning up new threads (one for each network connection), so using a single database connection from all of these threads isn't going to work.

  2. #2
    Join Date
    Jun 2010
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Removing database connections

    Ohhh... You do not need it at all
    Just let the QSqlDatabase non-pointer member object to be removed by finishing your class destructor.
    Then Qt will close the connection automatically for you
    See this point at Qt documentation:
    QSqlDatabase::~QSqlDatabase ()

    Destroys the object and frees any allocated resources.

    If this is the last QSqlDatabase object that uses a certain database connection, the is automatically closed.

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

    Matt31415 (7th July 2010)

Similar Threads

  1. Multiple database connections
    By Matt31415 in forum Qt Programming
    Replies: 9
    Last Post: 4th June 2010, 10:32
  2. QSqlite, multiple connections to in memory database.
    By adzajac in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2010, 22:35
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. DLL Connections
    By TheGrimace in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2007, 15:39
  5. # of connections?
    By gfunk in forum Qt Programming
    Replies: 1
    Last Post: 2nd August 2006, 23:31

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.