Results 1 to 5 of 5

Thread: Creating a thread-safe library to access a database

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Creating a thread-safe library to access a database

    Hi! I need to create a library that should provide API to get content from a database. I would like to create this library thread-safe, because it is possible that the users will try to invoke the methods from within many different threads.

    To do this, I created a singleton class that accesses the database. That class have methods get*, set* etc... to respectively execute select, update etc... queries. To guarantee thread-safety, in each method I create the connection to the database, I set the connection options, I open the database etc... and, at the end of the method I close the connection to the database. This because the connection can only be used in the same thread that created it.

    My question is: is this the correct way or the best way of doing it?

    Thanks!
    Last edited by Luc4; 2nd July 2011 at 09:21.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a thread-safe library to access a database

    Creating and opening connection to DB is time consuming.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a thread-safe library to access a database

    I would have a pool of DB connections instead and just use whichever is free at the time.

  4. #4
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Creating a thread-safe library to access a database

    I understand. This is very interesting! And how would you do this? I mean, if creating the connection is time consuming, when should I create each connection to add it to the pool?

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Creating a thread-safe library to access a database

    The database itself ought to provide substantial locking mechanisms, even when barraged by multiple simultaneous queries. There's no point, in terms of the database, in duplicating this capability on the client side. In fact, it will probably wind up hurting performance, since many queries can safely be handled in parallel otherwise, and bottlenecking them into a serial arrangement throws this capability away.

    Your DB documentation almost certainly has a section discussing such things.

    Squidge's suggestion is much better. By using a client-side thread pool, you can keep the number of threads constant and prevent saturating the local environment, while still allowing multiple requests simultaneously.

    Although I still don't see why simply allowing each thread to make its own independent connections and queries would cause problems.

Similar Threads

  1. thread-safe
    By babymonsta in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2010, 10:18
  2. Why is QMutex Thread Safe?
    By Kind Lad in forum Newbie
    Replies: 3
    Last Post: 22nd February 2010, 04:46
  3. About the QSound,is thread safe?
    By cspp in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2009, 14:26
  4. Is a QProcess thread safe in Qt4?
    By Jay_D in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2009, 16:38
  5. What makes something not thread safe?
    By tgreaves in forum Newbie
    Replies: 9
    Last Post: 20th February 2009, 20:16

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.