Under the high concurrency of java, the front desk is purchased and the background is modified at the same time, how can there be no dirty data in the design?

java code, using mongodb database, business scenarios are several items, which can be purchased at the front desk, and the backend may modify inventory or online and offline goods at the same time. How to design to ensure that dirty data will not be generated. I am a novice, and my idea at present is to write a locking method in the service layer,

public String updateint type String id int sum int status{
    synchronized (id.intern()) {
        switchtype{
            case 1: //type1sum
                break;
            case 2: //type2sum()status
                break; 
            default:
                break;        
        }
    }
}

but this is not very good either. If there is a negative possibility of reducing the number in the background, how do you do it, seniors? I haven"t been working for long and I don"t have any ideas for QAQ

.
Feb.28,2021

first of all, in principle, each order corresponds to a snapshot of the item, that is, no matter how the product information is changed after purchase, the order itself will not be affected. Secondly, if the modification of the commodity information itself involves multiple database operations, then the transaction must be completed; if the transaction cannot be realized, the commodity must be temporarily offline, and then go online again after the modification is completed.


first of all, I agree with the view of fabricated belief, whether the business under review is reasonable and whether the logic is correct
there are several ways to prevent concurrency:

  1. makes use of the optimistic locking mechanism of the database itself, which is generally suitable for situations where the probability of concurrent conflict is small, that is, optimistically that it will not cause conflict. If the probability of modification conflict of the same item caused by high concurrency is very high, optimistic lock is obviously not appropriate, which will lead to the failure of one party's operation
  2. .
  3. implement memory-level serialization through code, that is, the locking mechanism used by the landlord. This method increases the complexity of the code and affects performance.
  4. is not recommended.
  5. Serialize concurrent operations through message queues, and then use appropriate logic to process unified merchandise while modifying (in fact, it has become serial)

in addition to using locks in the code, take a look at using optimistic locks at the database level: https://blog.csdn.net/yown/ar.


just use the optimistic lock that comes with mongo. The specific examples are as follows:

-sharp 
{ 
    "_id" : ObjectId("5abf230cafcbb810e52f2e6e"), 
    "count" : 2
}

-sharp :

db.demo.update(
    {count: {$gt: 0},"_id" : ObjectId("5abf230cafcbb810e52f2e6e")},
    {$inc :{count :-1} }
)

-sharp  update  count =0  update 
-sharp 
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-1eaea57-47fb3.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-1eaea57-47fb3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?