当前位置:网站首页>Notes on the development of raspberry pie (17): QT multi-user connection operation on raspberry pie 4b+ MySQL database synchronization (pessimistic lock of single data)
Notes on the development of raspberry pie (17): QT multi-user connection operation on raspberry pie 4b+ MySQL database synchronization (pessimistic lock of single data)
2022-06-21 23:18:00 【Changsha Red fatty QT】
If the article is original , Reprint please indicate the source of the original
This article blog address :https://hpzwl.blog.csdn.net/article/details/125188715
Red fat man ( Red Imitation ) The complete blog of : Development technology collection ( contain Qt Practical technology 、 Raspberry pie 、 The three dimensional 、OpenCV、OpenGL、ffmpeg、OSG、 Single chip microcomputer 、 The combination of software and hardware and so on ) Ongoing update …( Click on the portal )
Raspberry pie development column
Last one :《 Notes on raspberry pie development ( sixteen ): Raspberry pie 4B+ install mariadb database (mysql Open source branch ) And test the basic operation 》
Next : Coming soon …
Preface
Installed mysq database , Finally, in order to realize the synchronization of multi-user and multi process operations on a raspberry pie , Avoid some errors in data concurrency , This chapter installs remote services and describes how to use Qt Pessimistic lock for update operation , Example of synchronous query from the command line .
Other operating
Here is just a little mention , For details, please refer to the blogger's raspberry pie series blog , Very detailed .
Remote login interface
sudo apt-get install tightvncserver
sudo apt-get install xrdp
sudo service xrdp restart
sudo ufw allow 3389
sudo service ufw restart
And then you can use window Remote desktop login :
Default user name :pi
Default password :raspberry


install qt5
sudo apt-get install qt5-default
sudo apt-get install qtcreator
After installed , There are more remote desktop programs qtcreator 了 :


Create an interface project , And then run :
( Compile faster than a few years ago 3B+ faster , Test it in the subsequent development process , Can I ignore 3B+ Cross compilation of )

Check the database driver :

No, mysql Database driver for .
sudo apt-get install libqt5sql5-mysql

Qt operation mariadb database
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setPort(3306);
db.setDatabaseName("data");
db.setUserName("root");
db.setPassword("a1234567");
if(db.open())
{
LOG << "Succeed to open db";
}else{
LOG << "Failed to open db:" << db.lastError().text();
return;
}
QString cmd = "select * from student;";
QSqlQuery query = db.exec(cmd);
while(query.next())
{
LOG << query.value(0).toString()
<< query.value(1).toString()
<< query.value(2).toString()
<< query.value(3).toString();
}

Multi user operation
It is intended for multi-user operation , Then you need to add a read lock when reading , Write lock is required when writing .
Two users read a record in the database at the same time , At this time, the user A The value of one of the fields was modified and submitted , Later users B This field has also been modified , user B The submission of will overwrite the user A Submitted value .

Lock type
Pessimistic locking
Every time I go to get the data , Very pessimistic , I think it will be modified by others , So it's locked when you get the data . in short , Shared resources are used by only one thread at a time , Other threads are blocking , After the first thread runs out, transfer the resources to other threads .synchronized and ReentranLock And so on are the embodiment of the pessimistic lock thought .
Optimism lock
Every time I go to get the data , They are optimistic , I don't think it will be modified . So I don't lock it every time , But in When it's updated , We will see if others have updated the data during this period , If there is any update, get it again , Then judge , Cycle all the time , Until you get the data that has not been modified .(mysql You need to realize the optimistic lock yourself ).
for update Use scenarios ( Pessimistic locking )
for update You can add an exclusive lock to a row of data in the database . When the operation of a transaction is not completed , Other transactions can read but cannot write or update .
If the project has requirements for the accuracy of certain data , And the project has concurrency ( Not necessarily high concurrency ), You need to use for update.
such as : user A Use the balance to buy goods , At this time, the user B To the user A Initiate a transfer , If it happens at the same time , It may cause users to A Final balance error . You need to use for update Lock the data to prevent errors .
In this case , Even if concurrency is small , But there is also a certain probability that you will encounter , The error of the balance is intolerable even if it is a penny short , So this particular scenario , Even if it is not highly concurrent , You should also use for update Avoid problems .
for update usage
begin;
select * from XXX where XXX for update;
...
commit;
for update Must be in a transaction to take effect .
Qt test
Use 127.0.0.1 Of ip Connect ( Local connection )
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setPort(3306);
db.setDatabaseName("data");
db.setUserName("root");
db.setPassword("a1234567");
if(db.open())
{
LOG << "Succeed to open db";
}else{
LOG << "Failed to open db:" << db.lastError().text();
return;
}
if(db.transaction())
{
QString cmd = "select * from student for update;";
QSqlQuery query = db.exec(cmd);
while(query.next())
{
LOG << query.value(0).toString()
<< query.value(1).toString()
<< query.value(2).toString()
<< query.value(3).toString();
}
for(int index = 0; index < 10; index++)
{
QThread::sleep(1);
LOG << "sleep:" << index;
}
if(!db.commit())
{
LOG << "Failed to commit";
}
}

thus , Our lock was successfully added , It is convenient for everyone to start to develop the synchronous development of multi-user process operation database .
Using LAN ip Connect ( Remote connection )
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("192.168.0.103");
db.setPort(3306);
db.setDatabaseName("data");
db.setUserName("root");
db.setPassword("a1234567");
if(db.open())
{
LOG << "Succeed to open db";
}else{
LOG << "Failed to open db:" << db.lastError().text();
return;
}
if(db.transaction())
{
QString cmd = "select * from student for update;";
QSqlQuery query = db.exec(cmd);
while(query.next())
{
LOG << query.value(0).toString()
<< query.value(1).toString()
<< query.value(2).toString()
<< query.value(3).toString();
}
for(int index = 0; index < 10; index++)
{
QThread::sleep(1);
LOG << "sleep:" << index;
}
if(!db.commit())
{
LOG << "Failed to commit";
}
}
Can't connect :

At this time, you need to modify the configuration :
sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf

Restart the service :
systemctl restart mysql.service

Continue testing , Here is how to use the local area network with raspberry pie ip( You can't connect before 127.0.0.1):

Successful connection , And then on the LAN pc On the connection , But the transaction cannot be opened :

Revise it :

This is actually related to mysql Drive related , Because I got it a few years ago libmysql.dll and libmysqld.dll, I won't delve into this , It can be operated remotely .
Last one :《 Notes on raspberry pie development ( sixteen ): Raspberry pie 4B+ install mariadb database (mysql Open source branch ) And test the basic operation 》
Next : Coming soon …
This article blog address :https://hpzwl.blog.csdn.net/article/details/125188715
边栏推荐
- How to associate the QR code of wechat applet and realize the integration of two codes
- keep-alive的使用注意点
- Uni app advanced style framework / production environment [Day10]
- Analysis of 43 cases of MATLAB neural network: Chapter 9 associative memory of discrete Hopfield Neural Network -- number recognition
- Uniapp play video, download video to mobile photo album, add download progress bar function (step on pit record)
- mongo 内存占用过大被系统自动关闭问题
- MVN deploy bat files for multiple modules
- Uniapp solves the cross domain problem of Google browser and runs in Google browser
- Readjustment of move protocol beta to expand the total prize pool
- WPF startup with parameters
猜你喜欢

An example of data format conversion in MySQL

Mongo uses too much memory and is automatically shut down by the system
![[understanding pointer] advanced level of pointer](/img/56/2db57773ed2d0c71b05455dd5e0d6c.png)
[understanding pointer] advanced level of pointer

H5 wechat authorized login (wechat authorized login of the uniapp web version)

uniapp微信授权之 有个别用户 无法正常授权

4. esp8266 displays DHT11 temperature and humidity parameters in real time through OLED

Translation software Bob installation tutorial

Software testing Q & A

Migration of stm32f407 program to stm32f429

4. ESP8266通过OLED实时显示DHT11温湿度参数
随机推荐
Record the abnormal task status caused by an MQ concurrent consumption
Analysis of 43 cases of MATLAB neural network: Chapter 9 associative memory of discrete Hopfield Neural Network -- number recognition
[electronic scheme design] PCBA solution for alcohol tester
Uwp tablet inkcanvas
Operation and maintenance specification: process template for online fault handling
About LG (n!) Asymptotically compact supremum of
WSL 2 的安装过程(以及介绍)
If you spend 200W to buy traffic, it is better to start at 0 cost and make an independent station with high private domain operation income!
It is said that the design of high concurrency system is not difficult at all!
[highly recommended] markdown grammar
SIGIR2022 | 对话式推荐系统中的用户偏好建模
Qt中常用的窗体
Ah ah ah ah ah ah
啊啊啊啊啊啊啊
mysql中数据格式转换的一个示例
Apache shardingsphere 5.1.2 release | new driving API + cloud native deployment to create a high-performance data gateway
牛客月賽-環上食蟲
Distributed database uses logical volume to manage storage expansion
Danfoss inverter maintenance vlt5000/vlt6000/vlt8000
STM32F407程序移植到STM32F429