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. Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?
Forum Updated to NodeBB v4.3 + New Features

Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 1.1k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by
    #3

    The problem is in QSqlDatabase::addDatabase.
    One of my objects contain QSqlDatabase::addDatabase assigned to property.
    How to use QSqlDatabase::addDatabase properly in QThread and quit correctly?

    jsulmJ 1 Reply Last reply
    0
    • B bogong

      The problem is in QSqlDatabase::addDatabase.
      One of my objects contain QSqlDatabase::addDatabase assigned to property.
      How to use QSqlDatabase::addDatabase properly in QThread and quit correctly?

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

      @bogong said in Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?:

      One of my objects contain QSqlDatabase::addDatabase assigned to property

      Why? You should not do that (is also stated in the documentation).

      "How to use QSqlDatabase::addDatabase properly in QThread and quit correctly?" - call it in the thread, but do NOT store it in a variable.

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

      B 1 Reply Last reply
      3
      • jsulmJ jsulm

        @bogong said in Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?:

        One of my objects contain QSqlDatabase::addDatabase assigned to property

        Why? You should not do that (is also stated in the documentation).

        "How to use QSqlDatabase::addDatabase properly in QThread and quit correctly?" - call it in the thread, but do NOT store it in a variable.

        B Offline
        B Offline
        bogong
        wrote on last edited by
        #5

        @jsulm ??? Why so? In every example that i found assigning:

        • https://forum.qt.io/topic/24927/solved-qthread-and-qsqldatabase-example
        • https://forum.qt.io/topic/77278/qsqldatabase-multiple-threads
        • https://overcoder.net/q/1684904/использование-соединения-qsqldatabase-в-qtconcurrent-run-пул-псевдо-соединений
        • https://coderoad.ru/23201320/qt-одновременных-MySQL-запросов-в-разных-потоках-сбой

        Even official doc showing the same ...

        jsulmJ Christian EhrlicherC 2 Replies Last reply
        0
        • B bogong

          @jsulm ??? Why so? In every example that i found assigning:

          • https://forum.qt.io/topic/24927/solved-qthread-and-qsqldatabase-example
          • https://forum.qt.io/topic/77278/qsqldatabase-multiple-threads
          • https://overcoder.net/q/1684904/использование-соединения-qsqldatabase-в-qtconcurrent-run-пул-псевдо-соединений
          • https://coderoad.ru/23201320/qt-одновременных-MySQL-запросов-в-разных-потоках-сбой

          Even official doc showing the same ...

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

          @bogong https://doc.qt.io/qt-5/qsqldatabase.html
          "Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior."

          Which official examples do that?

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

          B 1 Reply Last reply
          3
          • B bogong

            @jsulm ??? Why so? In every example that i found assigning:

            • https://forum.qt.io/topic/24927/solved-qthread-and-qsqldatabase-example
            • https://forum.qt.io/topic/77278/qsqldatabase-multiple-threads
            • https://overcoder.net/q/1684904/использование-соединения-qsqldatabase-в-qtconcurrent-run-пул-псевдо-соединений
            • https://coderoad.ru/23201320/qt-одновременных-MySQL-запросов-в-разных-потоках-сбой

            Even official doc showing the same ...

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @bogong said in Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?:

            In every example that i found assigning:

            You really should read those threads instead simply posting links. At least in 3 of the 4 it was mentioned/the solution to not store the QSqlDatabase instance as member!

            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
            3
            • jsulmJ jsulm

              @bogong https://doc.qt.io/qt-5/qsqldatabase.html
              "Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior."

              Which official examples do that?

              B Offline
              B Offline
              bogong
              wrote on last edited by bogong
              #8

              @jsulm said in Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?:

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

              Thx. I've got forgotten about difference between QSqlDatabase::addDatabase and QSqlDatabase::database. I have an application that hasn't been using threads. Everything in on thread - main. This application handling few standalone DBs based on SQLITE. And all have been working perfectly. Now I see where I am wrong.

              What is the correct way if I want to handle multiple DBs in multiple threads?
              Something like:

              QThread1
              	|- DB1
              	|- DB2
              QThread2
              	|- DB3
              QThread3
              	|- DB4
              
              jsulmJ 1 Reply Last reply
              0
              • B bogong

                @jsulm said in Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?:

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

                Thx. I've got forgotten about difference between QSqlDatabase::addDatabase and QSqlDatabase::database. I have an application that hasn't been using threads. Everything in on thread - main. This application handling few standalone DBs based on SQLITE. And all have been working perfectly. Now I see where I am wrong.

                What is the correct way if I want to handle multiple DBs in multiple threads?
                Something like:

                QThread1
                	|- DB1
                	|- DB2
                QThread2
                	|- DB3
                QThread3
                	|- DB4
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @bogong said in Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?:

                What is the correct way if I want to handle multiple DBs in multiple threads?

                Use distinct connection names and create the connections in the threads where you're going to use them (do not share same connection across threads).

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

                B 1 Reply Last reply
                3
                • jsulmJ jsulm

                  @bogong said in Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?:

                  What is the correct way if I want to handle multiple DBs in multiple threads?

                  Use distinct connection names and create the connections in the threads where you're going to use them (do not share same connection across threads).

                  B Offline
                  B Offline
                  bogong
                  wrote on last edited by
                  #10

                  @jsulm But where should be created DB's itself?

                  jsulmJ 1 Reply Last reply
                  0
                  • B bogong

                    @jsulm But where should be created DB's itself?

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

                    @bogong What do you mean by DB itself? Do you mean physical database?

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

                    B 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @bogong What do you mean by DB itself? Do you mean physical database?

                      B Offline
                      B Offline
                      bogong
                      wrote on last edited by bogong
                      #12

                      @jsulm I mean what is the correct way to initiate DB by QSqlDatabase::addDatabase at first time? I understand that QSqlDatabase::database should be called from QThread. But where to initiate it? In main thread or it might be initiated anywhere? Do I need to do something additional to QSqlDatabase::addDatabase?

                      jsulmJ 1 Reply Last reply
                      0
                      • B bogong

                        @jsulm I mean what is the correct way to initiate DB by QSqlDatabase::addDatabase at first time? I understand that QSqlDatabase::database should be called from QThread. But where to initiate it? In main thread or it might be initiated anywhere? Do I need to do something additional to QSqlDatabase::addDatabase?

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

                        @bogong said in Deleting all objects in QThread if one of objects contain QSqlDatabase::addDatabase. How?:

                        But where to initiate it?

                        As I said: in the thread where you want to use the connection. I also already mentioned that you should NOT share db connections across different threads. So, create the connection in the thread where you want to use it and use this connection only in that thread...

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

                        1 Reply Last reply
                        2
                        • B Offline
                          B Offline
                          bogong
                          wrote on last edited by
                          #14

                          @jsulm @Christian-Ehrlicher Thx. Issue solved. Everything works fine for me now. Example published here

                          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