One , Optimistic lock

 
Always thought there would be no concurrency problems , Every time I go to fetch data, I always think that no other thread will modify the data , So it won't lock , But when updating, it will judge whether other threads have modified the data before this , Version number mechanism or CAS Operation implementation .

 version mode
: Generally, a data version number is added to the data table version field , Indicates the number of times the data has been modified , When data is modified ,version Value will be increased by one . When thread A To update data values , Read data at the same time version value , When an update is submitted , If you just read version Value is in the current database version Update when values are equal , Otherwise, retry the update operation , Until the update succeeds .

core SQL code :
update table set x=x+1, version=version+1 where id=#{id} and
version=#{version};

 CAS Operation mode : Namely compare and swap perhaps compare and
set, Three operands involved , Memory value of data , Expected value , New value . When updates are needed , Determine whether the current memory value is equal to the previous value , If equal , Update with new value , Retry if failed , Generally, it is a spin operation , I.e. constant retries .

One , Pessimistic lock

 
Always assume the worst , Every time data is fetched, it is assumed that other threads will modify , So it's all locked ( Read lock , Write lock , Line lock, etc ), When other threads want to access data , Blocking pending required . It can be realized by database , Such as line lock , Read lock, write lock, etc , Lock before operation , stay Java in ,synchronized Is also a pessimistic lock .

Technology
©2019-2020 Toolsou All rights reserved,
JS How to operate java Realize the function of grabbing red packets C Language programming to find a student's grade The United Nations 《 Glory of Kings 》 Please go to the studio : To save the earth Dialogue between apple and Nissan suspended ,Apple Car How's it going ?CSS architecture design China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour First knowledge MySQL Comprehensive review ( dried food )2021 year 1 Monthly programmer salary statistics , average 14915 element How to use it quickly html and css Write static page