Results 1 to 7 of 7

Thread: QSqlDatabase best practices with long-running application

  1. #1
    Join Date
    Dec 2010
    Posts
    35
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSqlDatabase best practices with long-running application

    I have a Qt-based application that communicates with both MySQL and SQLServer. The application is long-running (started 4 - 5 times/daily for 90 minutes at a time) and the users interact with the database periodically - mostly reading, but some updates.

    The production environment consists of one DB server and 1 - 10 versions of the software running simultaneously on multiple computers connected to the same DB server.

    I have a question about QSqlDatabase best practices n database connections.

    Is it best to open a database only when necessary and close it quickly after the user action (e.g., viewing and updating a record)?

    Or is it OK to keep the DB open for the duration of the life of the application?

    I have a data object graph which I need to create and persist during the application. The data model is pretty basic - lots of master-detail type of relationships. I traverse the object graph and map n insert the objects to the appropriate DB tables. The 'insert or update' DB operation can be entered at any point in the object graph. Consequently, each type of object manages (via a simple class hierarchy) the connection to the DB (open on DAO creation and close on destruction). I create a transaction for the 'insert or update' DB operation as well.

    I'm wondering if I need to use some sort of reference counter for DB open/close operations to ensure the DB only closed after the last DAO class exits...

    Ideas?

  2. #2
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QSqlDatabase best practices with long-running application

    This is a very interesting issue. Have you meanwhile any suggestions or directions to this situation?
    I am also interested in solving this issue, cause I am in the same situation. I would like to program a client with access to a server DB. So 1 or 5 seems to be no prob but what about 100 clients? What are the Strategies on the App side and on the other hand the DB side??

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlDatabase best practices with long-running application

    Keep the database open if your database engine is configured to handle simoultanous connections from all the clients.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Dec 2010
    Posts
    35
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlDatabase best practices with long-running application

    Quote Originally Posted by codeman View Post
    This is a very interesting issue. Have you meanwhile any suggestions or directions to this situation?
    No, no other suggestions.

    I implemented short-lived database connections. Works well. In general:

    - 'main() starts n I 'QSqlDatabase::addDatabase()' and set connection properties and 'open()' it.
    - call QApplication::exec();
    - in main(), after the 'a.exec()' returns, I call 'QSqlDatabase::removeDatabase()'

    In the application, I call 'QSqlDatabase::database(name, true)' at the point I need to perform some DB activity. After the DB activity, I call 'QSqlDatabase::close()'.

    Within the application when I want to update or insert objects from my data graph to the DB, I have very small data access objects (DAOs) that mirror my data graph objects and traverse the tree n update or insert objects at each point in the graph.

    Since I can 'persist' the graph starting at any point (assuming the parent of the starting point is already persisted and I have a key), I implemented a map of databases n reference counters (map<string, QAtomicInt>). As I get or open a DB I increment the reference counter for the DB. As I close it, I decrement the counter. when the counter reaches zero, I actually do the QSqlDatabase::close().

    Quote Originally Posted by codeman View Post
    I would like to program a client with access to a server DB. So 1 or 5 seems to be no prob but what about 100 clients? What are the Strategies on the App side and on the other hand the DB side??
    There are multiple strategies here. If you have lots of CRUD operations on your DB, you may want to consider a single point of entry into the DB (e.g., some single service like a web services). That way accesses to the DB are serialized.

    We don't do that so we have to very carefully code our application to ensure that whatever object instance (e.g., record in the DB) is to be updated we start a transaction, retrieve it, update it and flush it back to the DB then commit the transaction.

    Hope that helps. I've been working with Java n Hibernate for many years and I sort of like what Hibernate does so I implemented a Hibernate-lite-lite-lite DB infrastructure using Qt.

  5. #5
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QSqlDatabase best practices with long-running application

    Thank you very much for sharing your experiences. But what in detail means this:

    Since I can 'persist' the graph starting at any point (assuming the parent of the starting point is already persisted and I have a key), I implemented a map of databases n reference counters (map<string, QAtomicInt>). As I get or open a DB I increment the reference counter for the DB. As I close it, I decrement the counter. when the counter reaches zero, I actually do the QSqlDatabase::close().
    Do you connect from one client to n DB´s?? Perhaps my english is bad ;o)) I attach a sample. What do you think about this aproach.
    Attached Files Attached Files
    Last edited by codeman; 15th October 2011 at 02:15.

  6. #6
    Join Date
    Dec 2010
    Posts
    35
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlDatabase best practices with long-running application

    Quote Originally Posted by codeman View Post
    Thank you very much for sharing your experiences. But what in detail means this:
    Here:
    Qt Code:
    1. MyDatabase::getDatabase (const string & name)
    2. throw (FlowException)
    3. {
    4. QSqlDatabase _db = QSqlDatabase::database(QString::fromStdString(name), true);
    5. if ( _db.isValid() && _db.isOpen()) {
    6. DB_NAME_MAP::iterator dItr = _dbCountMap.find (name);
    7. if (dItr != _dbCountMap.end()) {
    8. bool refRet = dItr->second.ref();
    9. } else {
    10. QAtomicInt newCounter;
    11. newCounter.ref();
    12. _dbCountMap[name] = newCounter;
    13. }
    14. return _db;
    15. }
    16. throw DataAccessLayerException("database '" + name + "' not open or valid");
    17. }
    18.  
    19. void
    20. MyDatabase::close(const string & name)
    21. {
    22. QSqlDatabase _db = QSqlDatabase::database(QString::fromStdString(name), false);
    23.  
    24. // only close if reference counter is 0.
    25.  
    26. bool derefRet = false;
    27. DB_NAME_MAP::iterator dItr = _dbCountMap.find (name);
    28. if (dItr != _dbCountMap.end()) {
    29. derefRet = dItr->second.deref();
    30. } else {
    31. throw DataAccessLayerException ("Unknown database");
    32. }
    33. if (! derefRet) {
    34. _db.close();
    35. _dbCountMap.erase (name);
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by codeman View Post
    Do you connect from one client to n DB´s??
    Yes. I support multiple database connections w/in the same running application.

  7. #7
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QSqlDatabase best practices with long-running application

    hmm ok thank you very much.

Similar Threads

  1. Problem: the Application Takes very long time to build
    By Ma7moud El-Naggar in forum Qt Programming
    Replies: 5
    Last Post: 20th November 2010, 06:26
  2. Replies: 2
    Last Post: 5th October 2010, 08:20
  3. Cancelling Long-running Print/Print Preview
    By ChrisW67 in forum Newbie
    Replies: 4
    Last Post: 16th June 2009, 23:05
  4. long running paint + menu hole
    By hvw59601 in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2007, 13:04
  5. QSqlDatabase Time out no network to long....
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2007, 00:41

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.