How to ensure both performance and data consistency in the case of concurrency by judging the result after query and then operating the data?

< H2 > problem description < / H2 >
  1. each user"s record in the order table of the database has 15 states ( 12 status codes represent on-the-way status, 3 status codes represent final status ).
  2. The state between
  3. data can be transferred.
  4. requires that the active status of the same user can have only one record, and the final state can have more than one record.
  5. how to ensure data integrity
< H2 > the environmental background of the problem and what methods you have tried < / H2 >
  1. Code flow: query the data before inserting it into the database. If you query the data that the current user is not in progress, insert it; if you query the data that the current user has a status in progress, you will not insert it.
  2. but when the concurrency of the same user is high, it is well known that the data is not rigorous.
  3. in the case of a single node, Synchronize (synchronized) can be used to solve the problem of data integrity, but when Synchronize is used, it also blocks the operation of all other users, resulting in poor performance.
< H2 > what result do you expect? What is the error message actually seen? < / H2 >
I expect some great god to come up with a better solution, which is better than Synchronize (synchronized) "s method, and also ensures data integrity.

look forward to!

Apr.11,2021

1: use the unique index of the database.
2: distributed locks.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4bced-593e9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4bced-593e9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?