当前位置:网站首页>QT operation on SQLite database multithreading

QT operation on SQLite database multithreading

2022-06-21 11:13:00 yantuguiguziPGJ

(562 Bar message ) QT in sqlite Multithreading operations 4 Notes _qtsqlite Multithreading ,qtsqlite Multithreading -QT Document resources -CSDN library icon-default.png?t=LA92https://download.csdn.net/download/storylsl/4176013?spm=1001.2014.3001.5503

Multithreaded operation on database :

The program written at the beginning did not notice the impact of multithreading on the database and the whole program , Although the program did not report an error , However, a lot of problems were exposed during the long-term debugging in the later stage .

First of all, the database deadlock can not be operated after the program runs for two hours , Locking the database operation in the early stage still cannot solve this problem . After searching for information , This problem has been solved . So here is a summary of the processing that should be performed when multithreading data operations :

This is a standard multi-threaded database operation ( I think so ), It will be explained according to this code .

1 Each thread or timer database driver under multithreading needs to be loaded independently

Before writing the program , When multiple classes of a program need to operate on the same database , The result of the processing is that the QsqlDatabase Pass it as a parameter to the required subclass . This is not a big problem in single process programs . But in multithreading , Due to the particularity of threads , This will cause problems . So the best way is to , In every thread or timer , Separate database driven loading , Define a different name . And in use ,QsqlQuery The variable must be set to the existing QsqlDatabase As a parameter :

2 Multi thread database operation must be locked

This is usually done , It should be noted that if there is a database open judgment in the program , Be sure to remember that opening failed return Unlock when , Otherwise, it will be very easy to cause deadlock .

3 It is better to open the database every time , Turn off when you're done , Don't keep it open

There will be a temporary file when the database is opened , Transfer all data to temporary files for operation , If it's not closed , This temporary file will not be cleared , That is to say, your other thread is about to operate the database , There is no data in the database , therefore , Be sure to open and close each time you operate .

4 The point is QsqlQuery  The problem with this variable

When I was writing code tests in the early stage, I found that , After adopting the above method , Under multithreading, the database still has to be pondered after two hours , After a long struggle, I began to change my mind and print the debugging information , Found a code segment with the most frequent database operations for debugging , Estimated and printed the debugging information when closing the database , It is found that although the parameter result is -1, That is, the parameter of normal shutdown , However, the last two parameters of the error message for closing the database are empty , The last two parameters in my print are the description of the wrong shutdown of the database . This is very puzzling , The only thing that can go wrong is QsqlQuery 了 , But I add clear() And setting different variable names to test the discovery , Printing information is still abnormal , Finally, after consulting an expert, the solution is to make a statement every time QsqlQuery Variable time , Add a pair {} Curly braces are used to set the scope life cycle of this variable , After executing this code, let it end its own life , Sure enough , The printing information is normal this time . But the effect of this variable on memory is still unknown , Why do you have such a problem . You may have an epiphany when you improve your level in the future , But now as long as we can solve the problem .

I won't say much about other database operations . Solve the problem this time , Learned a lot , To better understand a truth , Code without errors is not necessarily good code . In addition, it is the key to quickly locate the problem this time , The process of debugging is to constantly write and print information , Guessing alone is not enough .

This is my summary of some problems encountered by databases under multithreading , Deficiencies will be added later

My level is also limited , A fresh graduate , I just hope I can help my friends who have the same problems . ha-ha

原网站

版权声明
本文为[yantuguiguziPGJ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211057155112.html