Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Evaluating QSqlQuery accross different threads
Forum Updated to NodeBB v4.3 + New Features

Evaluating QSqlQuery accross different threads

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
6 Posts 4 Posters 1.5k Views 2 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.
  • Mike NeuhausM Offline
    Mike NeuhausM Offline
    Mike Neuhaus
    wrote on last edited by Mike Neuhaus
    #1

    Hi,

    My application uses the QtSqlite driver to interact with a database. The QT docs states that no database connection can be moved between threads. Because of this limitation I created a serializer to execute all the queries from a single thread. Then I notify other threads via the signal/slot system (or call a blocking function that waits until the scheduled query is executed succesfully) once the query is done. The serializer creates a local QSqlQuery object that is used to execute the query string. Once the execution is done I copy the QSqlQuery object to the designated thread which (and destroy the local copy before any other thread is able to access the copied QSqlQuery object) scheduled it, basically moving the executed QSqlQuery object to another thread you might say. However I have some memory leak issues, when the QSqlQuery object (the one that is copied from the serializer thread to its own thread) is destroyed the resources are not free'd. I checked the internal reference counter to see how many references there are when the particular query is destroyed and I can confirm this is a value of '1' when the QSqlQuery is being destroyed. I'm not sure why this could be a problem as the query is created in the same thread where I created my database connection. I'll hope someone can shed some light on this subject.

    Kind Regards,

    Mike

    jsulmJ 1 Reply Last reply
    0
    • Mike NeuhausM Mike Neuhaus

      Hi,

      My application uses the QtSqlite driver to interact with a database. The QT docs states that no database connection can be moved between threads. Because of this limitation I created a serializer to execute all the queries from a single thread. Then I notify other threads via the signal/slot system (or call a blocking function that waits until the scheduled query is executed succesfully) once the query is done. The serializer creates a local QSqlQuery object that is used to execute the query string. Once the execution is done I copy the QSqlQuery object to the designated thread which (and destroy the local copy before any other thread is able to access the copied QSqlQuery object) scheduled it, basically moving the executed QSqlQuery object to another thread you might say. However I have some memory leak issues, when the QSqlQuery object (the one that is copied from the serializer thread to its own thread) is destroyed the resources are not free'd. I checked the internal reference counter to see how many references there are when the particular query is destroyed and I can confirm this is a value of '1' when the QSqlQuery is being destroyed. I'm not sure why this could be a problem as the query is created in the same thread where I created my database connection. I'll hope someone can shed some light on this subject.

      Kind Regards,

      Mike

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

      @Mike-Neuhaus Somehow this sounds complicated. Why should all these threads deal with QSqlQuery? Why don't you let one thread access the database and provide data (not QSqlQuery!) to all the other threads?

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

      Mike NeuhausM 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Mike-Neuhaus Somehow this sounds complicated. Why should all these threads deal with QSqlQuery? Why don't you let one thread access the database and provide data (not QSqlQuery!) to all the other threads?

        Mike NeuhausM Offline
        Mike NeuhausM Offline
        Mike Neuhaus
        wrote on last edited by
        #3

        @jsulm This is indeed a solution, I could cache the results and give the other threads access to the cached results. However the serializer is an integration in an existing system, the current software is using QSqlQuery extensively that's the only reason why I want to remain using the QSqlQuery class.

        kshegunovK 1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Hi @Mike-Neuhaus,

          I suspect you'll need to delve into the internal details of QSqlQuery to figure out if what you're doing is possible. Subscribe to the Interest mailing list (http://lists.qt-project.org/mailman/listinfo/interest ) and ask there -- Qt engineers are active there.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • Mike NeuhausM Mike Neuhaus

            @jsulm This is indeed a solution, I could cache the results and give the other threads access to the cached results. However the serializer is an integration in an existing system, the current software is using QSqlQuery extensively that's the only reason why I want to remain using the QSqlQuery class.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            What you want depends heavily on the driver in use, whether or not the SQLite driver can be used from different threads, I can't say. But the problem isn't with the SQL query class itself, it's with the driver (and the QSqlDatabase that holds a reference to the driver).

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            1
            • Mike NeuhausM Offline
              Mike NeuhausM Offline
              Mike Neuhaus
              wrote on last edited by
              #6

              Thanks for all the answers, I ended up posting it on the interest. For now I implemented the cached results solution.

              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