In Windows 10, Yii2.0, and MySQL, the unique index of the combination is realized, and the process of data repeated writing in high concurrent situations is solved.

SQL 语句已成功运行

1. In the production environment, the data in the skill data table is repeatedly written, as shown in Figure 1

在生产环境上出现了技能数据表中的数据被重复写入的情况
Figure 1

2. However, before writing, it is judged whether the record exists. The combination of GAME_CODE and TEAM_CODE should be the only one, as shown in Figure 2

但是在写入之前是做了记录是否存在的判断的,game_code与team_code的组合应该是唯一的才是了
Figure 2

3. It is recommended to check the website first: https://www.shuijingwanwq.com/2017/04/18/1568/ , due to the mechanism of the program requested by POST, the POST file of AB only supports the parameters of the request, and cannot simulate the real scene. The real scene should be that the parameters of each request are different, so the bug in the production environment cannot be reproduced

4. By commenting out the conditions for judging whether the record exists, each request is inserted, and the data is inserted, and the scenario where there are multiple skill data in a game is simulated, as shown in Figure 3

通过在注释掉判断记录是否存在的条件,实现每次请求,数据皆是插入了,模拟出一场比赛存在多条技能数据的场景
Figure 3

5. Based on Postman, two consecutive requests with different parameters have been executed, and finally there are 4 records in MySQL, as shown in Figure 4 and 5

基于postman,连续执行了两次参数不一样的请求
Figure 4
最终MySQL中存在4条记录
Figure 5

6. In order to avoid the occurrence of this situation, it is decided to modify the table structure in mysql, delete the 4 records in the table, and put GAME_CODE and TEAM_C ODE is added as the unique index of the combination, select GAME_CODE and TEAM_CODE in phpMyAdmin, and click the unique button, as shown in Figure 6

将game_code与team_code添加为组合唯一索引,在phpMyAdmin中选择game_code与team_code,点击唯一按钮
Figure 6

7. The SQL statement has been successfully run, and the unique index of the combination is added successfully, as shown in Figure 7 and 8

ALTER TABLE `KQ_GAME_TEAM_SKILL` add unique( `GAME_CODE`, `TEAM_CODE`);

SQL 语句已成功运行
Figure 7
添加组合唯一索引成功
8

8. Based on GII, regenerate new model files, as shown in Figure 9

基于Gii,重新生成新的模型文件
Figure 9

9. Insert a new line in Rules(), as shown in Figure 10

在rules()中新插入了一行
Figure 10

10. Based on Postman, two consecutive requests with different parameters are executed, and an error is reported when executing the second time, as shown in Figure 11

基于postman,连续执行了两次参数不一样的请求,执行第2次的时候报错
Figure 11

11. The error message has been processed, and the original error message is printed out, as shown in Figure 12

报错信息已经经过处理,打印出原始的报错信息
Figure 12

12. The original error message: $gameteamskill->Errors is as follows, as shown in Figure 13

原始的报错信息:$gameTeamSkill->errors 如下
Figure 13

13. Check MySQL at this time. There are only 2 records in the skill data corresponding to a game, and only 1 in a team, which is in line with expectations, as shown in Figure 14

此时查看MySQL,一场比赛所对应的技能数据只存在2条记录,一个球队只存在1条了的,符合预期
Figure 14

14. Even if the bug in the production environment is not simulated through AB pressure testing, through the realization of adding a unique index to mysql, in theory, the reproduction of this bug is prevented, but the code display is not a number, which needs to be optimized and processed.

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.