当前位置:网站首页>QT operation on SQLite database multithreading
QT operation on SQLite database multithreading
2022-06-21 11:13:00 【yantuguiguziPGJ】
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
边栏推荐
- 求你了,别在高并发场景中使用悲观锁了!
- qmlbook学习总结
- 618掘金数字藏品?Burberry等奢侈品牌鏖战元宇宙
- Unable to access gcr IO solutions
- STL summary
- 03. Redis actual battle: meeting goddess nearby by geo type
- is not allowed to connect to this mysql server
- map.values()转为List和ArrayList的复制
- Citus 11 for Postgres is completely open source and can be queried from any node (citus official blog)
- Regression analysis - basic content
猜你喜欢
随机推荐
Quick sorting, simple and easy to understand principle description, with C code implementation,
The advanced process resistance of Intel and TSMC is increasing, and Chinese chips are expected to shorten the gap
【100个 Unity实用技能】| 游戏中获取鼠标点击的坐标,并将游戏对象移动到鼠标的点击位置
Mqtt of NLog custom target
秘密法宝
Yum source server configuration
numpy | 插入不定长字符数组测试OK
wangeditor封装插件初步
Use of jobservice
leetcode 第一题——两数之和
QML入门到进阶
Kotlin - Sequence 序列
数据可视化入门
Revenue of Fortinet will increase by 29% in 2021 founder Xieqing talks about the future of network security industry
Nature子刊 | 周集中团队揭示长期气候变暖导致草地土壤微生物多样性降低
Handling of legal instruction problems
巴比特 | 元宇宙每日必读:微信或首次以“涉数字藏品二级交易”为由封禁一公众号,平台新规也对此提出警告...
Concept of naive Bayes
从零走进软件开发的世界
Kotlin - I/O流
https://download.csdn.net/download/storylsl/4176013?spm=1001.2014.3001.5503



![Fastapi web framework [pydantic]](/img/e1/290a8a6a978b9fb56a9c86f1734c45.png)
![[ZZ] OWT server: audio and video forwarding diagram](/img/c8/3f97865d88c295592709e03d6298e8.png)



