Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-89528

Cannot use QSqlDataBase in QThread

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2: Important
    • Resolution: Invalid
    • Affects Version/s: 5.11, 6.0.0
    • Fix Version/s: None
    • Component/s: SQL Support
    • Labels:
      None
    • Platform/s:
      Linux/X11, Windows

      Description

      I have multithreaded application. In main thread it creates default db connection

      void createDefaultDbConnection(const QJsonObject& settings) {
          auto hostName = settings.value("DB_host").toString();
          auto hostPort = settings.value("DB_port").toInt();
          auto databaseName = settings.value("DB_name").toString();
          auto userName = settings.value("DB_user").toString();
          auto password = settings.value("DB_pass").toString();
          
          auto db = QSqlDatabase::addDatabase("QPSQL");
          db.setHostName(hostName);
          db.setPort(hostPort);
          db.setDatabaseName(databaseName);
          db.setUserName(userName);
          db.setPassword(password);
          db.setConnectOptions("connect_timeout=3");    if (db.open()) {
              btInfo << QObject::tr("Соединение с базой данных установлено");
          }
          else {
              btError << QObject::tr("НЕ удалось установить соединение с базой данных, ошибка '%1'").arg(db.lastError().text());
          }
      }

       Then in main thread it creates 4 threads at start and 4 "db workers".

      for (int i = 0; i < DbWorkersCount; ++i) {
       auto workerThread = new QThread(this);
       mDbWorkersThreads << workerThread;
       workerThread->start();
      auto dbWorker = new BtDbWorker();
       dbWorker->moveToThread(workerThread);
       connect(workerThread, &QThread::finished, dbWorker, &QObject::deleteLater);
      QObject::connect(dbWorker, &BtDbWorker::response, this, &BtServer::onDbResponse, Qt::QueuedConnection);
       QObject::connect(dbWorker, &BtDbWorker::signal, this, &BtServer::onDbSignal, Qt::QueuedConnection);
       QObject::connect(dbWorker, &BtDbWorker::addPreviewTask, mPreviewProcessor, &PreviewProcessor::addPreviewTask, Qt::QueuedConnection);
      QMetaObject::invokeMethod(dbWorker,
       "start",
       Qt::QueuedConnection,
       Q_ARG(const QJsonObject&, settings.rootJsonObject()));
      mFreeDbWorkers << dbWorker;
       }
      

       

      In method "start" of each db worker i create unique db connection.

      void DbWorker::start(const QJsonObject& settings) {
        auto hostName = settings.value("DB_host").toString();
        auto hostPort = settings.value("DB_port").toInt();
        auto databaseName = settings.value("DB_name").toString();
        auto userName = settings.value("DB_user").toString();
        auto password = settings.value("DB_pass").toString();
      
        m_db = QSqlDatabase::addDatabase("QPSQL", QUuid::createUuid().toString());
        m_db.setHostName(hostName);
        m_db.setPort(hostPort);
        m_db.setDatabaseName(databaseName);
        m_db.setUserName(userName);
        m_db.setPassword(password);
        m_db.setConnectOptions("connect_timeout=3");
        m_db.open();
      }

      So its done correctly, as i get it. After that, in main thread requests to "db workers" passed through QMetaObject::invokeMethod :

      auto worker = mFreeDbWorkers.takeLast();
      QMetaObject::invokeMethod(worker,
                  "request",
                  Qt::QueuedConnection,
                  Q_ARG(BtRequest, req));
      

      But once sql query executed in "db worker", i get this message :

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

      However, it seems to complete my sql request, query.exec() returns 'true'.

      This application work fine on Qt 5.5.1

      But on Qt 5.11 and Qt 6 i get this message.

      The question is : is it an error, warning? Does my db interraction really executes in thread in witch it was created? or not? Or its fault message?

       

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            mabrand Mark Brand
            Reporter:
            feudal_lord Paul Mazurov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes