Problems with using QT sqlite
-
I used QT sqlite database function with QT version 5.9.9 early,but ,for some reasons I migrate my program to QT version 5.14.2. Some strange things happened. I create a global object to excute such as "select,insert,update ..." operattions and return to the caller a local object eigher QSqlQuery or QSqlRecord,the caller use a local object to stored the object, the procedure like:
QSqlQuery ExecuteSqlQuery(QString sql){
QSqlQuery result;
//........
return result;
}caller:
void queryData(void){
QSqlQuery result;
result = ExecuteSqlQuery("select * from table_driver");
}when I try query data from database in multithread with QT5.14.2,segment fault is catched.but it is run well with QT 5.9.9,.
Is there any diffrent between the two versions? -
Yes,I agree with you,but what make me confused is that the code run well witch version 5.9.9,
A little thing called luck. All it takes is a difference in class code, thread timing/scheduling and two shared users of the object do, or do not, stand on each others toes.
As @Sgaist said, you need to call have a connection created and maintained in each thread that will use the database. That means using the connectionName parameter in your QSqlDatabase::addDatabase() call in each thread to identify different connections. Your arrangement of a free function to create and execute a QSqlQuery needs to change to create the QSqlQuery object using the correct QSqlDatabase instance and, probably, must also be executed in the correct thread.
-
@coderKnight said in Problems with using QT sqlite:
when I try query data from database in multithread with QT5.14.2,segment fault is catched.but it is run well with QT 5.9.9,.
Is there any diffrent between the two versions?Nothing of substance. You are quite probably falling foul of undefined behaviour when using QSqlDatabase in a multi-threaded environment.
Threads and the SQL Module -
@coderKnight said in Problems with using QT sqlite:
when I try query data from database in multithread with QT5.14.2,segment fault is catched.but it is run well with QT 5.9.9,.
Is there any diffrent between the two versions?Nothing of substance. You are quite probably falling foul of undefined behaviour when using QSqlDatabase in a multi-threaded environment.
Threads and the SQL Module@ChrisW67
Yes,I agree with you,but what make me confused is that the code run well witch version 5.9.9, perhaps I should set a lock when operate database ,because it behaved as if the QSqlDatabase returned a global object to me,if I do other operation in other thread,I make the global object dirty,I will try and post the test result here -
Hi,
That's why you should use one different connection to your database per thread. Create said connection in the thread that will use it.
-
Yes,I agree with you,but what make me confused is that the code run well witch version 5.9.9,
A little thing called luck. All it takes is a difference in class code, thread timing/scheduling and two shared users of the object do, or do not, stand on each others toes.
As @Sgaist said, you need to call have a connection created and maintained in each thread that will use the database. That means using the connectionName parameter in your QSqlDatabase::addDatabase() call in each thread to identify different connections. Your arrangement of a free function to create and execute a QSqlQuery needs to change to create the QSqlQuery object using the correct QSqlDatabase instance and, probably, must also be executed in the correct thread.
-
Yes,I agree with you,but what make me confused is that the code run well witch version 5.9.9,
A little thing called luck. All it takes is a difference in class code, thread timing/scheduling and two shared users of the object do, or do not, stand on each others toes.
As @Sgaist said, you need to call have a connection created and maintained in each thread that will use the database. That means using the connectionName parameter in your QSqlDatabase::addDatabase() call in each thread to identify different connections. Your arrangement of a free function to create and execute a QSqlQuery needs to change to create the QSqlQuery object using the correct QSqlDatabase instance and, probably, must also be executed in the correct thread.
@ChrisW67
Sorry back to you late, I got into trouble for another item. If I create diffrent connection for diffrent thread ,I have too much code to be modified,so I set a mutex lock before operate the database ,I have tested for two days, it runs well.But I use the old code to do some test by the version5.9.9 again and again,it still works well,I don't know the reason,
Thanks for all -
It's because of this change: https://code.qt.io/cgit/qt/qtbase.git/commit/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp?id=610a9e8f319eafcbcf193e4b90119a6f89d0caf9
You must not use a QSqlDatabase connection in more than one thread (and therefore this global sqlite lock was removed for performance reasons).
-
It's because of this change: https://code.qt.io/cgit/qt/qtbase.git/commit/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp?id=610a9e8f319eafcbcf193e4b90119a6f89d0caf9
You must not use a QSqlDatabase connection in more than one thread (and therefore this global sqlite lock was removed for performance reasons).
@Christian-Ehrlicher
Thank you so much for let me know the reason