From e729ac6dc5efd2d68e1a98f63b9cfec83d349ea7 Mon Sep 17 00:00:00 2001 From: David Faure Date: Fri, 6 Apr 2018 09:05:40 +0200 Subject: [PATCH] QSqlDatabase: Skip confusing thread warning on invalid QSqlDatabase If the db isn't valid, then that's the actual issue, not the fact that we're getting the same invalid db (with the same driver QSqlNullDriver) in multiple threads. Change-Id: I95490818ed78e741c3823e115f139c2cff01b0b1 Reviewed-by: Andy Shaw --- src/sql/kernel/qsqldatabase.cpp | 6 ++++-- tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/sql/kernel/qsqldatabase.cpp b/src/sql/kernel/qsqldatabase.cpp index 291b35f8a6..2c7b4b83db 100644 --- a/src/sql/kernel/qsqldatabase.cpp +++ b/src/sql/kernel/qsqldatabase.cpp @@ -229,12 +229,14 @@ QSqlDatabase QSqlDatabasePrivate::database(const QString& name, bool open) dict->lock.lockForRead(); QSqlDatabase db = dict->value(name); dict->lock.unlock(); - if (db.driver() && db.driver()->thread() != QThread::currentThread()) { + if (!db.isValid()) + return db; + if (db.driver()->thread() != QThread::currentThread()) { qWarning("QSqlDatabasePrivate::database: requested database does not belong to the calling thread."); return QSqlDatabase(); } - if (db.isValid() && !db.isOpen() && open) { + if (open && !db.isOpen()) { if (!db.open()) qWarning() << "QSqlDatabasePrivate::database: unable to open database:" << db.lastError().text(); diff --git a/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp b/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp index 4130b364f4..1f055e9c33 100644 --- a/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp +++ b/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp @@ -2374,6 +2374,9 @@ public slots: "QSqlDatabasePrivate::database: requested database does not belong to the calling thread."); QSqlDatabase db = QSqlDatabase::database(dbName); QVERIFY(!db.isValid()); + + QSqlDatabase invalidDb = QSqlDatabase::database("invalid"); + QVERIFY(!invalidDb.isValid()); QThread::currentThread()->exit(); } private: