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. [workaround] qt_sql_default_connection' is still in use --- again

[workaround] qt_sql_default_connection' is still in use --- again

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.9k 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.
  • K Offline
    K Offline
    kimf
    wrote on last edited by
    #1

    Hi

    I'm developing a small daemon, which at regular intervals (many times every hour) opens a database connection, inserts some records, and closes the connection again.

    Everything works great, except every time the database connection is closed, I get the very well known "qt_sql_default_connection' is still in use" message, some small memory leak and an open connection to the database (after some time I get "Too many connections QMYSQL:").

    The funny thing is : I do not have to use any query to get the error, all I have to do, is to connect to the database, and disconnect again!

    I have made this small test program, which also displays the error.

    I have a form with two buttons, one for connect, one for disconnect.

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSqlDatabase>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked() // connect
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("test");
    db.setUserName("user");
    db.setPassword("pwd");
    db.open();
    }

    void MainWindow::on_pushButton_2_clicked() // disconnect
    {
    QSqlDatabase::database().close();
    QSqlDatabase::removeDatabase(QSqlDatabase::database().connectionName());
    }
    @

    Any ideas ?

    I'm using QtCreator 5.1 and mysql 5.5.32 on Ubuntu 64 bit.

    Kim

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Badredin
      wrote on last edited by
      #2

      Hello Kimf,

      Try providing the database name (i.e. "test") to the call of the static QSqlDatabase::database() function in 'on_pushButton_2_clicked().

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kimf
        wrote on last edited by
        #3

        Hi Badredin

        Well that does remove the default connection error, but now I get an "duplicate connection name 'qt_sql_default_connection', old connection removed." error when I try to reconnect !

        Kim

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

          Hi,

          Just a design question, why not setup the database once and only open and close it ?

          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
          0
          • K Offline
            K Offline
            kimf
            wrote on last edited by
            #5

            Hi SGaist

            That solution has crossed my mind, but because of my in-experience with this kind of long running processes (it could be running un interrupted for months), I decided this approch would be best.

            What would you say is the better solution, one permanent connection, or a new one each time ?

            Kim

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

              AFAIK, you can go with one database setup at application startup and open/close the connection has needed. Just be sure that all the transaction to the database have been executed before closing the connection.

              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
              0
              • K Offline
                K Offline
                kimf
                wrote on last edited by
                #7

                I have had the program running all night without any problems, so I think this is the way I will go.

                Thank you SGaist.

                Just to satisfy my curiosity, does anyone know what's wrong with my test program ?

                Kim

                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