Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

sql

database

qt

I have a folder where i have a many databases. Some times may be deleted or added database to the folder. So I use QTimer and read all databases.

It is a my code:

this->timer = new QTimer(this);
this->timer->setInterval(15000);
connect(this->timer, &QTimer::timeout, this, [=]() {
    QString path = "C:\\Users\\User\\Desktop\\DAXI SMS SENDER\\SMSSenderAllBASE";
    //QString path = qApp->applicationDirPath() + "\\SMSSenderAllBASE";
    QDir recoredDir(path);
    QStringList allFiles = recoredDir.entryList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
    for (int i = 0; i < allFiles.size(); i++) {
        QString fullPath = path + "\\" + allFiles[i];
        QString connectionName = allFiles[i];
        connectionName = connectionName.remove(connectionName.size() - 4, 4);
        QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE", connectionName);
        db.setDatabaseName(fullPath);
        db.setHostName("localhost");
        db.setPort(3050);
        db.setUserName("SYSDBA");
        db.setPassword("masterkey");

        thrdHelperSendSMS *help = new thrdHelperSendSMS(db, this);
        connect(help, &thrdHelperSendSMS::helperFinished, this, [=](QString connectionName){
            QSqlDatabase t_db = QSqlDatabase::database(connectionName);
            t_db.close();
            QSqlDatabase::removeDatabase(connectionName);
            delete help;
        });
        help->run();
    }
});
this->timer->start();

Yes I'm sure that the helperFinished signal will happen and this time I will not have any connection with this base.

EDIT: If i remove

   thrdHelperSendSMS *help = new thrdHelperSendSMS(db, this);
   connect(help, &thrdHelperSendSMS::helperFinished, this, [=](QString connectionName){
       QSqlDatabase t_db = QSqlDatabase::database(connectionName);
       t_db.close();
       QSqlDatabase::removeDatabase(connectionName);
       delete help;
   });
   help->run();

example:

for (int i = 0; i < allFiles.size(); i++) {
    QString fullPath = path + "\\" + allFiles[i];
    QString connectionName = allFiles[i];
    connectionName = connectionName.remove(connectionName.size() - 4, 4);
    QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE", connectionName);
    db.setDatabaseName(fullPath);
    db.setHostName("localhost");
    db.setPort(3050);
    db.setUserName("SYSDBA");
    db.setPassword("masterkey");

    QSqlDatabase::removeDatabase(connectionName);
}

I have the same error.

like image 633
Tazo leladze Avatar asked Sep 10 '25 21:09

Tazo leladze


2 Answers

You don't use removeDatabase() correctly. The object of SqlDatabase needs to go out of scope first. See documentation.

Wrong use

QSqlDatabase db = QSqlDatabase::database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
QSqlDatabase::removeDatabase("sales"); // will output a warning

Correct use

{
    QSqlDatabase db = QSqlDatabase::database("sales");
    QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
}
// Both "db" and "query" are destroyed because they are out of scope
QSqlDatabase::removeDatabase("sales"); // correct

In the second example db will go out of scope after } and you will no longer see the error message QSqlDatabasePrivate::removeDatabase: connection 'myConnectionName' is still in use, all queries will cease to work

Please read the documentation carefully. The database stuff is sensible and every line needs to be checked carefully.

Also you have missing db.close(); - It makes sense to close the database before you remove it.

like image 104
user3606329 Avatar answered Sep 13 '25 11:09

user3606329


@user3606329's answer is right, but I add this possibility:

QSqlDatabase db = QSqlDatabase::database("sales");
{
    QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
    //use query
}
db = QSqlDatabase();
QSqlDatabase::removeDatabase("sales");
like image 29
Albertino80 Avatar answered Sep 13 '25 09:09

Albertino80