I have a 'small but long' task. On a user request, the query to database should happen, this query can run for some noticeable time.
So this task is moved to a thread with a run() like this:
void SubTask::run() {
if (!db2.open()) {
err = "Failed to open db connection" + connName;
qCritical() << err;
}
qry.exec( .... );
qry,clear();
db2.close();
}
void SubTask::run() {
const QString connName = QString::number((quintptr)QThread::currentThreadId());
QSqlDatabase db2 = QSqlDatabase::cloneDatabase(db, connName); // db is SQLite database created in the main thread
if (!db2.open()) {
err = "Failed to open db connection" + connName;
qCritical() << err;
}
QSqlQuery qry(db2);
qry.exec( .... );
qry,clear();
db2.close();
QSqlDatabase::removeDatabase(connName);
}
To copy to clipboard, switch view to plain text mode
It works fine, query is executing each time user requests it.
But in the console I have a warning:
QSqlDatabasePrivate::removeDatabase: connection '140551504512768' is still in use, all queries will cease to work.
QSqlDatabasePrivate::removeDatabase: connection '140551504512768' is still in use, all queries will cease to work.
To copy to clipboard, switch view to plain text mode
One such warning for each execution.
What am I doing wrong? How the connection should be closed correctly?
Bookmarks