Problems encountered by for update.

each commodity is limited to 1000 copies per day, and the robbery starts at 10:00. Select count from table where date = year, month, day, for update lock. How many copies have been sold on the same day? If it is not more than 1000, continue with the following purchase operation. The operation completes this table count+1. Then the commit transaction. The problem now is. More than one piece of data will appear on the same day.
clipboard.png

clipboard.png
this is the case. What should be done?

Feb.08,2022
The

ID column creates a unique index. An exception occurs in the data submitted after
, which can be retried later in the exception handling.


first of all, this description of Baidu is not correct. When the data does not exist, it will add a gap lock, in addition to table lock (IX), but do not use this method in the program, it is easy to produce deadlocks. In your case, date plus a unique index is the easiest way.

Menu