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. Accessing the same Database from different threads
Forum Updated to NodeBB v4.3 + New Features

Accessing the same Database from different threads

Scheduled Pinned Locked Moved Solved General and Desktop
sqlitedatabasethread
3 Posts 2 Posters 1.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.
  • D Offline
    D Offline
    davidesalvetti
    wrote on last edited by
    #1

    Hi guys,

    I know there is a lot about this problem but after searching on the net I didn't found an exhaustive answer.

    I have the main thread that has its own connection to the database and works well. Now I have to create a second thread that needs the access to the same database.

    if I understood well, if I create two different connections with two different names that point to the same database there won't be any problems.

    I'm using SQLite database and as default the libraries compiled by QT should have SQLITE_THREADSAFE=1 setting the threading mode to Serialized allowing multithread operations.

    what I didn't understand is what happens in the case where both the threads are trying to access at the same moment the same database (using two different connections)?

    Like:

    //main thread

    void thread1{
    ...
    QSqlQuery qry(db);

    qry.prepare("UPDATE Time SET Hour = 10, Min = 20 , Sec = 30");
    qry.exec();
    
    ...
    

    }

    void thread2{
    ...
    QSqlQuery qry(db_2);

    qry.prepare("SELECT rowid FROM Time WHERE Hour = 10 AND Min = 20 AND Sec = 30");
    qry.exec();
    
    ...
    

    }

    Are they both executed one after the other? If the answer is yes, I suppose that it's not possible to know wich one is executed first.

    Thank you for your time.

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

      Hi,

      No, it's not possible, the order of the execution of the threads is unknown.

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

      D 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        No, it's not possible, the order of the execution of the threads is unknown.

        D Offline
        D Offline
        davidesalvetti
        wrote on last edited by
        #3

        @SGaist
        Thank you for your answer!

        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