Mysql transaction concurrency problem solution

Mysql transaction concurrency problem solution

I encountered such a problem during development

A video watching record, when updated to 100, means it has been watched, and it will not be updated if there are subsequent requests.

turn out:

As a result, many data inside are problematic.

It is speculated that the following circumstances will lead to

The first request transaction is in execution and has not been committed (because it is sometimes difficult to reproduce locally, so I manually sleep for a few seconds in the program when processing the first record to achieve this effect)

The second request transaction has started to execute. At this time, the historical maximum value found is not 100, so it will be updated.

I looked up the solution online:

Pessimistic Lock

Direct lock row record

I tested this locally and it really works. One transaction starts but does not end, and the second transaction waits. However, it will cause a blocking state because of system concurrency. I dare not consider it, so I just record this method.

Manual simulation:

Execute the first transaction:

-- Video 100 BEGIN;

SELECT * FROM `biz_coursestudyhistory` WHERE sid = 5777166;

UPDATE biz_coursestudyhistory set studyStatus = 100,versionNO=versionNO+1 WHERE sid = 1 AND versionNO = 0;

-- commit ; ​​Do not execute first, annotate first, and only execute the above 

Then execute the second transaction:

BEGIN;
 
UPDATE biz_coursestudyhistory set studyStatus = 90,versionNO=versionNO+1 WHERE sid = 1 AND versionNO = 0;
 
SELECT * FROM `biz_coursestudyhistory` WHERE sid = 1 FOR UPDATE;
 
COMMIT;

You will find that you cannot succeed and are always in a waiting state.

View Locks

It is indeed locked. Here, as long as the commit of the first transaction is executed, the second transaction will be executed.

From this we can see that row locks can directly achieve the ideal data unification state. If one transaction is modified, other operations cannot be performed. I feel that this is more suitable for security projects such as banks.

Optimistic locking:

This is simpler and does not cause blocking

The way is to add the version number

var maxver = select max(version) from table

To update, use

update table set studystatus = xxx,version = version +1 where id =1 and version = maxver

Written words

INSERT into table (contentStudyID,courseWareID,studyStatus,studyTime,endTime)
SELECT 27047358,3163,100,333,NOW() FROM dual WHERE NOT EXISTS (SELECT 1 FROM table WHERE contentStudyID =27047358 AND
courseWareID = 3163
 )

In this way, when updating or writing, you can directly determine whether the data in the library exists. If it does not exist, it is used by other threads.

After changing to this writing style, using jmeter for multi-threaded testing, the initial multiple record updates were successful, but now only one record is successful, and the rest fail.

From inserting multiple records at the beginning, to only inserting one data later

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of using MySQL transaction features to implement concurrent and safe auto-increment ID
  • Solution to PHP+MySQL high-concurrency locked transaction processing problem
  • Can MySQL's repeatable read level solve phantom reads?
  • Detailed explanation of how MySQL solves phantom reads
  • Detailed explanation of MySQL phantom reads and how to eliminate them
  • MySQL Series 10 MySQL Transaction Isolation to Implement Concurrency Control
  • How to solve the phantom read problem in MySQL
  • mysql+mybatis implements stored procedure + transaction + multi-concurrent serial number acquisition
  • Detailed explanation of concurrent dirty read + non-repeatable read + phantom read in Mysql transactions

<<:  Example code for converting http to https using nginx

>>:  Using vue3 to implement counting function component encapsulation example

Recommend

Json advantages and disadvantages and usage introduction

Table of contents 1. What is JSON 1.1 Array liter...

Detailed explanation of Linux netstat command

Table of contents Linux netstat command 1. Detail...

Basic notes on html and css (must read for front-end)

When I first came into contact with HTML, I alway...

Summary of several implementations of returning to the top in HTML pages

Recently, I need to make a back-to-top button whe...

Vue virtual Dom to real Dom conversion

There is another tree structure Javascript object...

A brief discussion on Yahoo's 35 rules for front-end optimization

Abstract: Whether at work or in an interview, opt...

Docker online and offline installation and common command operations

1. Test environment name Version centos 7.6 docke...

How to set mysql permissions using phpmyadmin

Table of contents Step 1: Log in as root user. St...

Building a Redis cluster on Docker

Table of contents 1. Pull the image 2. Create a R...

Detailed explanation of CocosCreator optimization DrawCall

Table of contents Preface What is DrawCall How do...

A brief discussion on HTML ordered lists, unordered lists and definition lists

Ordered List XML/HTML CodeCopy content to clipboa...

Detailed explanation of the reasons why MySQL connections are hung

Table of contents 1. Background Architecture Prob...

Solution to Docker's failure to release ports

Today I encountered a very strange situation. Aft...

Docker enables seamless calling of shell commands between container and host

As shown below: nsenter -t 1 -m -u -n -i sh -c &q...