Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Looking for the best multithreading patterns for database access
Forum Update on Monday, May 27th 2025

Looking for the best multithreading patterns for database access

Scheduled Pinned Locked Moved Unsolved General and Desktop
multithreadingdatabasesignal & slot
9 Posts 4 Posters 3.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abforce
    wrote on last edited by
    #1

    I've got a database class that is moved to a background thread by moveToThread. And then sets up a connection to the database server. Since QSqlDatabase should be used in the thread which set it up, so other parts of program should send their requests to this object and wait for response.

    For example my database class has a slot called getUserById which takes an ID and queries the database and emits getUserByIdCompleted. Generally talking, my class has lots of foo slots and fooCompleted signals.

    I could think of two problems with this method:

    • If two entities simultaneously emit signal to getUserById how they could distinguish their own response, when database entity emits getUserByIdCompleted?

    • AFAIK this way, every classes wanting to access database should declare lots of signals only intended to be connected to the database class? Am I right? For example if class Foo wants to query a user, it should have a signal called something like heyDatabaseGetUserById and then we should connect it to database's getUserById slot. I think that's bad practice to have every classes defined with such signals. Is it possible to send signal to the database class without caller class's such signals?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Are you looking for something like QxOrm ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Are you looking for something like QxOrm ?

        A Offline
        A Offline
        abforce
        wrote on last edited by
        #3

        @SGaist No. I'm not looking for ORMs. My database logic is already implemented. I'm looking for a multithread access pattern.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Something that is not clear from your description is the multithreaded part. It seems that you only have one thread currently.

          What exactly would be multi-threaded ? Also, a database is usually something synchronous so do you really need all these signals ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            Why are you moving database to background thread if you're wanting to access it all over the main thread? Is it that you send off queries and carry on and later pick up results? Or...?

            A 1 Reply Last reply
            0
            • SGaistS SGaist

              Something that is not clear from your description is the multithreaded part. It seems that you only have one thread currently.

              What exactly would be multi-threaded ? Also, a database is usually something synchronous so do you really need all these signals ?

              A Offline
              A Offline
              abforce
              wrote on last edited by
              #6

              @SGaist I currently have two threads: GUI thread and background thread. Database class is moved to background thread since database operations are I/O blocking.

              My problem is being unable to find a mechanism to send requests from GUI thread and receive responses when operations complete.

              1 Reply Last reply
              0
              • JonBJ JonB

                Why are you moving database to background thread if you're wanting to access it all over the main thread? Is it that you send off queries and carry on and later pick up results? Or...?

                A Offline
                A Offline
                abforce
                wrote on last edited by
                #7

                @JNBarchan Since my program is using a QML UI layer, the GUI thread should be kept highly responsive as it may be required to deliver smooth animations. That's why I've moved database operations to a background thread.

                For this purpose can I employ a callback pattern? Doing the operation in background and when it's done call the callback in GHI thread.

                JonBJ jsulmJ 2 Replies Last reply
                0
                • A abforce

                  @JNBarchan Since my program is using a QML UI layer, the GUI thread should be kept highly responsive as it may be required to deliver smooth animations. That's why I've moved database operations to a background thread.

                  For this purpose can I employ a callback pattern? Doing the operation in background and when it's done call the callback in GHI thread.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @abforce
                  You originally wrote:

                  so other parts of program should send their requests to this object and wait for response

                  If your main thread "waits for the response", then it's not very asynchronous?

                  I am a newcomer to Qt. I had expected, say, QSqlQuery, to offer a "begin query" function and a signal when data arrived back to read, which would have been my first thought for implementing asynchronous database calls, instead of calling synchronous functions from a background thread given that is problematic, but I don't see such a facility....?

                  Given which, I'll leave it to experts to help you further....

                  1 Reply Last reply
                  0
                  • A abforce

                    @JNBarchan Since my program is using a QML UI layer, the GUI thread should be kept highly responsive as it may be required to deliver smooth animations. That's why I've moved database operations to a background thread.

                    For this purpose can I employ a callback pattern? Doing the operation in background and when it's done call the callback in GHI thread.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @abforce said in Looking for the best multithreading patterns for database access:

                    For this purpose can I employ a callback pattern?

                    You can simply use signals/slots across threads

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved