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



  • 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



  • Hello Kimf,

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



  • 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


  • Lifetime Qt Champion

    Hi,

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



  • 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


  • Lifetime Qt Champion

    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.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.