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. Handling Database on Multiple Thread
QtWS25 Last Chance

Handling Database on Multiple Thread

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadsql databasec++multithreaddebugging
13 Posts 3 Posters 2.7k 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.
  • C Online
    C Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 27 Nov 2021, 15:25 last edited by
    #2

    Using a QThread for a single insert statement doesn't look very effective. The same goes for the serial communication - QSerialPort is async - no thread needed.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    M 1 Reply Last reply 27 Nov 2021, 16:02
    4
    • C Christian Ehrlicher
      27 Nov 2021, 15:25

      Using a QThread for a single insert statement doesn't look very effective. The same goes for the serial communication - QSerialPort is async - no thread needed.

      M Offline
      M Offline
      mvsri
      wrote on 27 Nov 2021, 16:02 last edited by mvsri
      #3

      @Christian-Ehrlicher There are multiple insert statements and I am using open close principal for database connection.
      Regarding Serialport Qt serial blocking example
      I referred this blocking example and I read and write data from serial thread and also planning to use wait conditions.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 27 Nov 2021, 16:17 last edited by
        #4

        delay logic is troublesome because it makes assumptions about response times, sometimes needlessly, and other times not long enough. Avoid that kind of programming when possible, and favor event sycronization instead.

        M 1 Reply Last reply 27 Nov 2021, 16:30
        2
        • K Kent-Dorfman
          27 Nov 2021, 16:17

          delay logic is troublesome because it makes assumptions about response times, sometimes needlessly, and other times not long enough. Avoid that kind of programming when possible, and favor event sycronization instead.

          M Offline
          M Offline
          mvsri
          wrote on 27 Nov 2021, 16:30 last edited by
          #5

          @Kent-Dorfman For serial communication most preferable way is to use async code with signals and slots. Understood the point here.
          But can you tell me when is preferred way to use above blocking example.

          Regarding Database connection can I use the thread where there are multiple insert statements and I’m using open close method here.

          Thank you!

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kent-Dorfman
            wrote on 27 Nov 2021, 16:40 last edited by
            #6

            open/close is expensive. insert/select based on prepared statements is cheap. move the open/close logic out of the thread and do mutext blocked sql functions if you chose a single connect. otherwise create a connection pool and have your thread use a free connection from the pool for every concurrent sql transaction.

            implementation is left as an exercise for the OP.

            M 1 Reply Last reply 28 Nov 2021, 07:08
            3
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 27 Nov 2021, 18:11 last edited by
              #7

              Even two or thread statements don't need a separate thread. Especially when the open/close is done in the run function as @Kent-Dorfman pointed out.
              Write a proper class which opens the db in the ctor and closes it in the dtor, implement the insert statements in a slot() and after you found out that this causes performance problems (which I doubt) then move this object to another thread.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              M 1 Reply Last reply 28 Nov 2021, 07:06
              1
              • C Christian Ehrlicher
                27 Nov 2021, 18:11

                Even two or thread statements don't need a separate thread. Especially when the open/close is done in the run function as @Kent-Dorfman pointed out.
                Write a proper class which opens the db in the ctor and closes it in the dtor, implement the insert statements in a slot() and after you found out that this causes performance problems (which I doubt) then move this object to another thread.

                M Offline
                M Offline
                mvsri
                wrote on 28 Nov 2021, 07:06 last edited by
                #8

                @Christian-Ehrlicher Thank you for the suggestion I will implement it and check the result.
                Although before proceeding my query is since I'm reading data from serialthread and emitting it to mainscreen and in mainscreen I'm displaying the values as well as storing the values in the database using databasethread. My point here is should I use a locking mechanism here when handling the data between threads?

                C 1 Reply Last reply 28 Nov 2021, 09:32
                0
                • K Kent-Dorfman
                  27 Nov 2021, 16:40

                  open/close is expensive. insert/select based on prepared statements is cheap. move the open/close logic out of the thread and do mutext blocked sql functions if you chose a single connect. otherwise create a connection pool and have your thread use a free connection from the pool for every concurrent sql transaction.

                  implementation is left as an exercise for the OP.

                  M Offline
                  M Offline
                  mvsri
                  wrote on 28 Nov 2021, 07:08 last edited by
                  #9

                  @Kent-Dorfman I will take your suggestions as well as @Christian-Ehrlicher suggestion and implement them.

                  1 Reply Last reply
                  0
                  • M mvsri
                    28 Nov 2021, 07:06

                    @Christian-Ehrlicher Thank you for the suggestion I will implement it and check the result.
                    Although before proceeding my query is since I'm reading data from serialthread and emitting it to mainscreen and in mainscreen I'm displaying the values as well as storing the values in the database using databasethread. My point here is should I use a locking mechanism here when handling the data between threads?

                    C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 28 Nov 2021, 09:32 last edited by Christian Ehrlicher
                    #10

                    @mvsri said in Handling Database on Multiple Thread:

                    My point here is should I use a locking mechanism here when handling the data between threads?

                    Don't use threads and you don't need a locking.
                    Use signals and slots properly and you don't need a locking even between threads.

                    https://doc.qt.io/qt-5/signalsandslots.html

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    M 1 Reply Last reply 28 Nov 2021, 14:54
                    3
                    • C Christian Ehrlicher
                      28 Nov 2021, 09:32

                      @mvsri said in Handling Database on Multiple Thread:

                      My point here is should I use a locking mechanism here when handling the data between threads?

                      Don't use threads and you don't need a locking.
                      Use signals and slots properly and you don't need a locking even between threads.

                      https://doc.qt.io/qt-5/signalsandslots.html

                      M Offline
                      M Offline
                      mvsri
                      wrote on 28 Nov 2021, 14:54 last edited by
                      #11

                      @Christian-Ehrlicher I rewrote the code and implemented everything with signals and slots, thank you for the suggestions. It pretty much sums up the questions I had.

                      I just need one suggestion though what is the real use case of blocking serial communication method Qt serial blocking example. When do we use such kind of code then?

                      C 1 Reply Last reply 28 Nov 2021, 15:21
                      1
                      • M mvsri
                        28 Nov 2021, 14:54

                        @Christian-Ehrlicher I rewrote the code and implemented everything with signals and slots, thank you for the suggestions. It pretty much sums up the questions I had.

                        I just need one suggestion though what is the real use case of blocking serial communication method Qt serial blocking example. When do we use such kind of code then?

                        C Online
                        C Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 28 Nov 2021, 15:21 last edited by
                        #12

                        @mvsri said in Handling Database on Multiple Thread:

                        When do we use such kind of code then?

                        I would say - mostly never. Maybe if there is really much data coming in from the serial port (e.g. in high-speed modes when you can get ~8MBit/s) to make sure all the data can be handled without killing the gui interaction. But as I said earlier - don't use threads until you really need them :)

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        4
                        • K Offline
                          K Offline
                          Kent-Dorfman
                          wrote on 30 Nov 2021, 17:50 last edited by
                          #13

                          to continue the point @Christian-Ehrlicher mentioned above, blocking serial tasks usually exist on server daemon applications, but not on anything with user interaction or a GUI.

                          1 Reply Last reply
                          3

                          11/13

                          28 Nov 2021, 14:54

                          • Login

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