Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. What care i need to take when my 2 different thread access same database ?
Qt 6.11 is out! See what's new in the release blog

What care i need to take when my 2 different thread access same database ?

Scheduled Pinned Locked Moved Solved C++ Gurus
15 Posts 4 Posters 4.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.
  • Q Qt embedded developer

    @J-Hilk said in What care i need to take when my 2 different thread access same database ?:

    SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads.

    I want elaboration of this statement

    " SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads."

    If meaning of this statement is " i can not use same database connection in multiple thread" then How to make it simultaneously access ?

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #4

    @Qt-embedded-developer
    I want an elaboration on how you made your database connections so far


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    Q 1 Reply Last reply
    1
    • J.HilkJ J.Hilk

      @Qt-embedded-developer
      I want an elaboration on how you made your database connections so far

      Q Offline
      Q Offline
      Qt embedded developer
      wrote on last edited by
      #5

      @J-Hilk i am opening database and closing database when i want to execute sqlite query

      jsulmJ 1 Reply Last reply
      0
      • Q Qt embedded developer

        @J-Hilk said in What care i need to take when my 2 different thread access same database ?:

        SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads.

        I want elaboration of this statement

        " SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads."

        If meaning of this statement is " i can not use same database connection in multiple thread" then How to make it simultaneously access ?

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

        @Qt-embedded-developer said in What care i need to take when my 2 different thread access same database ?:

        then How to make it simultaneously access

        Create one connection in each thread...

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

        1 Reply Last reply
        1
        • Q Qt embedded developer

          @J-Hilk i am opening database and closing database when i want to execute sqlite query

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

          @Qt-embedded-developer said in What care i need to take when my 2 different thread access same database ?:

          i am opening database and closing database when i want to execute sqlite query

          Quite inefficient if you execute many queries

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

          Q 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Qt-embedded-developer said in What care i need to take when my 2 different thread access same database ?:

            i am opening database and closing database when i want to execute sqlite query

            Quite inefficient if you execute many queries

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by Qt embedded developer
            #8

            @jsulm Can you suggest efficient way for many queries. i have to improve this inefficient design to make it efficient. So Please suggest me how to do it ?

            jsulmJ 1 Reply Last reply
            0
            • Q Qt embedded developer

              @jsulm Can you suggest efficient way for many queries. i have to improve this inefficient design to make it efficient. So Please suggest me how to do it ?

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

              @Qt-embedded-developer said in What care i need to take when my 2 different thread access same database ?:

              Can you suggest efficient way for many queries

              Simply open the connection once and use it for your queries. Close the connection when the app is closed.

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

              Q 2 Replies Last reply
              2
              • jsulmJ jsulm

                @Qt-embedded-developer said in What care i need to take when my 2 different thread access same database ?:

                Can you suggest efficient way for many queries

                Simply open the connection once and use it for your queries. Close the connection when the app is closed.

                Q Offline
                Q Offline
                Qt embedded developer
                wrote on last edited by
                #10

                @jsulm but this is the problem " no single database connection is used simultaneously in two or more threads" so i need to close the connection and open connection for simultaneous access.

                So what is other efficient solution to tackle this issue ?

                jsulmJ 1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  Qt embedded developer
                  wrote on last edited by
                  #11

                  How to know which thread handler open the data base in foreground thread and background thread ?

                  1 Reply Last reply
                  0
                  • Q Qt embedded developer

                    @jsulm but this is the problem " no single database connection is used simultaneously in two or more threads" so i need to close the connection and open connection for simultaneous access.

                    So what is other efficient solution to tackle this issue ?

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

                    @Qt-embedded-developer said in What care i need to take when my 2 different thread access same database ?:

                    but this is the problem

                    No, it's not.
                    I already wrote before: Create one connection in each thread.

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

                    1 Reply Last reply
                    2
                    • fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #13

                      https://sqlite.org/threadsafe.html
                      It sounds like sqlite is in mode 2. So internally sqlite is using mutexes.

                      C++ is a perfectly valid school of magic.

                      1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Qt-embedded-developer said in What care i need to take when my 2 different thread access same database ?:

                        Can you suggest efficient way for many queries

                        Simply open the connection once and use it for your queries. Close the connection when the app is closed.

                        Q Offline
                        Q Offline
                        Qt embedded developer
                        wrote on last edited by
                        #14

                        @jsulm i have opened the database connection and not close it . but it make my ui stuck to wait for release mutex lock

                        jsulmJ 1 Reply Last reply
                        0
                        • Q Qt embedded developer

                          @jsulm i have opened the database connection and not close it . but it make my ui stuck to wait for release mutex lock

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

                          @Qt-embedded-developer said in What care i need to take when my 2 different thread access same database ?:

                          but it make my ui stuck to wait for release mutex lock

                          Well, then analyse and fix your code - you have a dead-lock

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

                          1 Reply Last reply
                          1

                          • Login

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