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. requested database does not belong to the calling thread
Forum Updated to NodeBB v4.3 + New Features

requested database does not belong to the calling thread

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 7 Posters 8.4k 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.
  • N Offline
    N Offline
    nn26
    wrote on last edited by
    #1

    Hello,

    I am getting this error.

    QSqlDatabasePrivate::database: requested database does not belong to the calling thread
    

    In my application with database, I have 5 tables. I am using two tables with one thread (with plugins to application) and another three tables with second thread from same application.

    is this a problem due to same connection name for both thread? or something else?

    thanks.

    JonBJ 1 Reply Last reply
    0
    • N nn26

      Hello,

      I am getting this error.

      QSqlDatabasePrivate::database: requested database does not belong to the calling thread
      

      In my application with database, I have 5 tables. I am using two tables with one thread (with plugins to application) and another three tables with second thread from same application.

      is this a problem due to same connection name for both thread? or something else?

      thanks.

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

      @nn26
      You can only access a QSqlDatabase from the thread which created it/in which it lives.

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

        Hi,

        You need to create one connection per thread directly in the thread where you will use it.

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

        1 Reply Last reply
        3
        • N Offline
          N Offline
          nn26
          wrote on last edited by nn26
          #4
          QSqlDatabasePrivate::addDatabase: duplicate connection name'xxx', old connection removed
          

          so this is creating problem right?

          jsulmJ 1 Reply Last reply
          0
          • N nn26
            QSqlDatabasePrivate::addDatabase: duplicate connection name'xxx', old connection removed
            

            so this is creating problem right?

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

            @nn26 Don't use same connection name more than once

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

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

              Give each thread's connection a unique name. You can use the thread id for that for example.

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

              1 Reply Last reply
              3
              • U Offline
                U Offline
                UndeadBan
                wrote on last edited by UndeadBan
                #7

                I know this is an old topic but I've decided to leave a comment anyway, hopefully someone will respond.

                I have a mysql database connection pool implemented with QSqlDatabase like this:

                1. On startup, I open N database connections from the main thread
                2. During the application lifetime, there will be M long living threads
                3. When a thread needs database access it will synchronise access to the db pool and obtain a free connection (connection that no other thread has taken). That connection cannot be used by another thread, so there is no race condition. After it is done, it will mark the connection as free.

                Since number of threads in this case is usually higher than max. allowed number of connections, this enables me to have 100 threads using 10 mysql connections which are opened all the time.

                This worked well with Qt 5.9, but with Qt 5.15 I get the same error that "requested database does not belong to the calling thread".

                I understand the motives for this, but what are the alternatives to my implementation?

                These are the alternatives that came to mind, but they have their own issues:

                1. Keeping one persistent connection per thread would lead me to having too many connections
                2. Opening a connection only when thread needs it would slow things down, since each query would have to first open the connection, execute, and then close it. With the current implementation, an already opened connection is used.
                Christian EhrlicherC 1 Reply Last reply
                0
                • U UndeadBan

                  I know this is an old topic but I've decided to leave a comment anyway, hopefully someone will respond.

                  I have a mysql database connection pool implemented with QSqlDatabase like this:

                  1. On startup, I open N database connections from the main thread
                  2. During the application lifetime, there will be M long living threads
                  3. When a thread needs database access it will synchronise access to the db pool and obtain a free connection (connection that no other thread has taken). That connection cannot be used by another thread, so there is no race condition. After it is done, it will mark the connection as free.

                  Since number of threads in this case is usually higher than max. allowed number of connections, this enables me to have 100 threads using 10 mysql connections which are opened all the time.

                  This worked well with Qt 5.9, but with Qt 5.15 I get the same error that "requested database does not belong to the calling thread".

                  I understand the motives for this, but what are the alternatives to my implementation?

                  These are the alternatives that came to mind, but they have their own issues:

                  1. Keeping one persistent connection per thread would lead me to having too many connections
                  2. Opening a connection only when thread needs it would slow things down, since each query would have to first open the connection, execute, and then close it. With the current implementation, an already opened connection is used.
                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @UndeadBan said in requested database does not belong to the calling thread:

                  I understand the motives for this, but what are the alternatives to my implementation?

                  Open the connection in the thread you're using it.

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

                  U 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    @UndeadBan said in requested database does not belong to the calling thread:

                    I understand the motives for this, but what are the alternatives to my implementation?

                    Open the connection in the thread you're using it.

                    U Offline
                    U Offline
                    UndeadBan
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher Thank you for the response. Yeah, I understand that the connection will have to be opened from the thread where it's used. But having for example 100 threads where on average 10 of them are working with a database forces me to either have 100 connections, or frequently open and close connections. With the previous implementation, I could rotate 10 established connections between threads without the overhead of frequent open/close.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      drlaplace
                      wrote on last edited by
                      #10

                      I'm facing the same problem. Is there a solution now?

                      JonBJ 1 Reply Last reply
                      0
                      • D drlaplace

                        I'm facing the same problem. Is there a solution now?

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

                        @drlaplace
                        What *solution"? As @Christian-Ehrlicher wrote above

                        Open the connection in the thread you're using it.

                        It's no different now from that/how it was.

                        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