utilize select … where … for update Exclusive lock . Be careful : Other additional functions are basically consistent with the implementation of one , What needs to be noted here is “where name=lock ”,name Fields must be indexed , Otherwise, it will lock the watch . In some cases , For example, the watch is not big ,mysql The optimizer will not take this index , Causes lock table problems .
The biggest difference between the so-called optimistic lock and the front one is based on CAS thought , It's not mutually exclusive , No lock waiting and resource consumption , There is no concurrency conflict during operation , Only update version It's not until you fail that you realize . Our rush to buy 、 Second kill uses this realization to prevent oversold . Optimistic locking is achieved by increasing the incremental version number field .
Ideas ： Another service , utilize jdk Concurrency tools to control unique resources , Such as maintaining a concurrentHashMap, Other services for a key When a lock is requested , Ports exposed through the service , Send messages in the form of network communication , The server parses the message , take concurrentHashMap Medium key The corresponding value is set to true, Distributed lock request successful , It can be based on netty Communication call , Of course you want to use java Of bio、nio Or integration dubbo、spring cloud feign It's no problem to communicate
shortcoming ： this