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. How Can I use db in threads? Requested database does not belong to the calling thread.
Forum Updated to NodeBB v4.3 + New Features

How Can I use db in threads? Requested database does not belong to the calling thread.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.0k 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.
  • S Offline
    S Offline
    Subuday
    wrote on last edited by
    #1

    How to use database in threads? I create instance of db in main thread and give threads pointers. But it crashes, also I tried to create instance in Thread, it doesn't work. How Can I use db in threads?

    #ifndef THREAD_H
    #define THREAD_H
    
    #include <QThread>
    #include <QTcpSocket>
    #include <QRegularExpression>
    
    #include "dbmanager.h"
    
    class MyThread : public QThread
    {
      Q_OBJECT
    
      public:
        explicit MyThread(qintptr ID, QObject *parent = nullptr);
        ~MyThread();
    
        void run();
    
      signals:
        void error(QTcpSocket::SocketError socketerror);
    
      public slots:
        void readyRead();
        void disconnected();
    
      private:
        QTcpSocket *socket;
        qintptr socketDescriptor;
    
        DB_Manager * db;
    
        void analysis(QString httpRequest, QString& method, QString& uri, QString& body);
    };
    
    #endif // THREAD_H
    
    void MyThread::run()
    {
      socket = new QTcpSocket();
    
      if (!socket->setSocketDescriptor(this->socketDescriptor) )
      {
        emit error (socket->error());
        return;
      }
    
      connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()),Qt::DirectConnection );
      connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    
      exec();
    }
    
    void HttpServer::incomingConnection(qintptr socketDescriptor)
    {
        MyThread *thread = new MyThread(socketDescriptor, this);
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    
        thread->start();
    }
    

    In MyThread function:

    QJsonDocument dbody = QJsonDocument::fromJson(body.toLocal8Bit());
                QString password = dbody.object()["password"].toString();
    
                int id = db->getIdByLog(tname.toStdString(), password.toStdString());
    
                if(id != 0)
                {
                    sendLog(true, id);
                }
                else
                {
                    sendLog(false, 0);
                }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What is DB_Manager ?
      Where do you allocate it ?
      How is it implemented ?

      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
      1

      • Login

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