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. Find what is using a `QSqlDatabase` connection
Forum Updated to NodeBB v4.3 + New Features

Find what is using a `QSqlDatabase` connection

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

    Here my code:

    Orders::Orders(QObject *parent) : QObject{parent}
    {
            QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "orders");
            db.setHostName("localhost");
            db.setDatabaseName("database");
            db.setUserName("user");
            db.setPassword("password");
            db.setConnectOptions("MYSQL_OPT_RECONNECT=TRUE;");
            _isConnected = db.open();
        
            if (_isConnected)
            {
                _model = new QSqlTableModel(this, db);
                _model->setTable("orders");
                _model->setEditStrategy(QSqlTableModel::OnManualSubmit);
                _model->select();
            }
        }
    
    Orders::~Orders()
    {
        if (_model) _model->clear();
        QSqlDatabase db = QSqlDatabase::database("orders");
        db.close();
        QSqlDatabase::removeDatabase("orders");
    }
    

    I don't have any QSqlDatabase nor QSqlQuery class member. I always create them inside the functions, example:

    void Orders::setOrderState(QString machine, QString id, DbOrderStates state)
    {
        QSqlDatabase db = QSqlDatabase::database("orders");
        QString sql = QString("UPDATE orders SET states_id=:state WHERE id=:id AND machine=:machine");
    
        QSqlQuery query(db);
        query.prepare(sql);
        query.bindValue(":state", static_cast<int>(state));
        query.bindValue(":id", id);
        query.bindValue(":machine", machine);
        query.exec();
    }
    

    Still, it complains when I close the application:

    QSqlDatabasePrivate::removeDatabase: connection 'orders' is still in use, all queries will cease to work.

    I can't understand what is still in use... is there a way to find out what is using the connection?

    JonBJ 1 Reply Last reply
    0
    • M Mark81

      Here my code:

      Orders::Orders(QObject *parent) : QObject{parent}
      {
              QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "orders");
              db.setHostName("localhost");
              db.setDatabaseName("database");
              db.setUserName("user");
              db.setPassword("password");
              db.setConnectOptions("MYSQL_OPT_RECONNECT=TRUE;");
              _isConnected = db.open();
          
              if (_isConnected)
              {
                  _model = new QSqlTableModel(this, db);
                  _model->setTable("orders");
                  _model->setEditStrategy(QSqlTableModel::OnManualSubmit);
                  _model->select();
              }
          }
      
      Orders::~Orders()
      {
          if (_model) _model->clear();
          QSqlDatabase db = QSqlDatabase::database("orders");
          db.close();
          QSqlDatabase::removeDatabase("orders");
      }
      

      I don't have any QSqlDatabase nor QSqlQuery class member. I always create them inside the functions, example:

      void Orders::setOrderState(QString machine, QString id, DbOrderStates state)
      {
          QSqlDatabase db = QSqlDatabase::database("orders");
          QString sql = QString("UPDATE orders SET states_id=:state WHERE id=:id AND machine=:machine");
      
          QSqlQuery query(db);
          query.prepare(sql);
          query.bindValue(":state", static_cast<int>(state));
          query.bindValue(":id", id);
          query.bindValue(":machine", machine);
          query.exec();
      }
      

      Still, it complains when I close the application:

      QSqlDatabasePrivate::removeDatabase: connection 'orders' is still in use, all queries will cease to work.

      I can't understand what is still in use... is there a way to find out what is using the connection?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Mark81
      If you try a standalone program with just Orders::Orders() & Orders::~Orders() in it do you still get the message? My first thought is that you do have a QSqlQuery or similar left lying around somewhere.

      Hang on! I think it is your Orders::~Orders() which is the issue. You cannot QSqlDatabase::removeDatabase("orders") in the same scope as QSqlDatabase db = QSqlDatabase::database("orders");. You need:

          {
              QSqlDatabase db = QSqlDatabase::database("orders");
              db.close();
          }
          QSqlDatabase::removeDatabase("orders");
      

      This is even shown in https://doc.qt.io/qt-6/qsqldatabase.html#removeDatabase.

      M 1 Reply Last reply
      2
      • JonBJ JonB

        @Mark81
        If you try a standalone program with just Orders::Orders() & Orders::~Orders() in it do you still get the message? My first thought is that you do have a QSqlQuery or similar left lying around somewhere.

        Hang on! I think it is your Orders::~Orders() which is the issue. You cannot QSqlDatabase::removeDatabase("orders") in the same scope as QSqlDatabase db = QSqlDatabase::database("orders");. You need:

            {
                QSqlDatabase db = QSqlDatabase::database("orders");
                db.close();
            }
            QSqlDatabase::removeDatabase("orders");
        

        This is even shown in https://doc.qt.io/qt-6/qsqldatabase.html#removeDatabase.

        M Offline
        M Offline
        Mark81
        wrote on last edited by Mark81
        #3

        @JonB I read that paragraph dozen of times but I didn't realize the braces are intended to be literal! I understood it's just to point out I need to place the variables inside a function!

        Anyway it still wasn't enough!
        I had to add:

        delete _model;
        

        even if the docs says:

        Clears the model and releases any acquired resource.

        1 Reply Last reply
        1
        • M Mark81 has marked this topic as solved on
        • M Mark81 has marked this topic as unsolved on
        • M Mark81 has marked this topic as solved on

        • Login

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