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. Multithreading and sqllite

Multithreading and sqllite

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.8k Views
  • 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.
  • S Offline
    S Offline
    schouwen
    wrote on last edited by
    #1

    I am switching from qt 5.7 to 5.10. In my code I have in memory database (sqlite) with a shared connection between multiple threads. Maybe not the best idea but it worked fine (in 5.7). In 5.10 though it crashed in the threads so, understanding that shared connection is not the way to go I tried the recommended QSqlDatabase::addDatabase per thread of course with a unique name for each connection. Something like this

    auto p = QSqlDatabase::addDatabase("QSQLITE", Identity::newAnonymousName("ilwisobjects_databaseconnection").toLatin1());
    p.open();

    Before the threads are started the main thread fills the database with 20 tables but the connection(in a thread) says there are 0 tables. Probably I am misunderstanding something but from reading what other people wrote this seems correct.

    jsulmJ 1 Reply Last reply
    0
    • S schouwen

      I am switching from qt 5.7 to 5.10. In my code I have in memory database (sqlite) with a shared connection between multiple threads. Maybe not the best idea but it worked fine (in 5.7). In 5.10 though it crashed in the threads so, understanding that shared connection is not the way to go I tried the recommended QSqlDatabase::addDatabase per thread of course with a unique name for each connection. Something like this

      auto p = QSqlDatabase::addDatabase("QSQLITE", Identity::newAnonymousName("ilwisobjects_databaseconnection").toLatin1());
      p.open();

      Before the threads are started the main thread fills the database with 20 tables but the connection(in a thread) says there are 0 tables. Probably I am misunderstanding something but from reading what other people wrote this seems correct.

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

      @schouwen You're using different databases in different threads. Filling tables in one thread will not affect databases in other threads. As far as I know SQLite is NOT thread safe, if this is true it is in general a bad idea to access same database from more than one thread without serialising the queries.

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

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Just as a note.
        It seems it can be thread safe but i never tried it.
        https://sqlite.org/threadsafe.html
        So the big questions is if compiled with this flag or
        if possible to insert SQLITE_CONFIG_MULTITHREAD
        at runtime.
        Also, this is "raw" Sqlite, and im not sure how its related to using it via Qt and its classes.

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

          Hi,

          The key is "in memory database". By default it uses a private cache that is only visible to the thread that opened the connection.

          See the SQLite documentation about that here.

          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
          2
          • S Offline
            S Offline
            schouwen
            wrote on last edited by
            #5

            Ah, thank you. The inmemory database is good hint. That would explain what I am seeing.

            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