This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging

⭐ August more text challenge day 5 ⭐, learn MybatisPlus, welcome friends to learn 😁

Code Mantis shrimp is a sand sculpture and interesting young boy, like most friends like listening to music, games, of course, in addition to the interest of writing, Emm… There is still a long time to go. Let’s work hard together 🌈

Welcome friends to pay attention to my public number: JavaCodes, although the name with Java, but the scope of more than Java field oh 😁, look forward to your attention ❤

The original link⭐MybatisPlus study notes ⭐ (five) to achieve optimistic locking mechanism

Before the order

MybatisPlus column

⭐MybatisPlus learning notes ⭐ (1) Environment setup and entry HelloWorld

⭐MybatisPlus study Notes ⭐ (2) CRUD full set of detailed explanation

⭐MybatisPlus learning notes ⭐ (3) To achieve logical deletion, paging

⭐MybatisPlus study Notes ⭐ (4) Conditional constructor Wrapper method detailed explanation

Look at the MybatisPlus module for this link



Overview of optimistic locking

Optimistic Locking The Optimistic Locking mechanism is more relaxed than the pessimistic Locking mechanism. Pessimistic locking is implemented in most cases by relying on the database’s locking mechanism to ensure maximum exclusivity of operations. But with that comes a huge overhead on database performance, especially for long transactions, which is often unaffordable. Optimistic locking mechanism solves this problem to some extent. Optimistic locking is mostly implemented based on the data Version recording mechanism. What is data version? Adding a version identity to the data is typically done by adding a “version” field to the database table in a versioning solution based on database tables. When the data is read out, the version number is read together, and when the data is updated, the version number is incremented by one. In this case, the version data of the submitted data is compared with the current version information recorded in the corresponding database table. If the version number of the submitted data is equal to the current version number of the database table, the data is updated; otherwise, the data is regarded as outdated.


2. Main application scenarios

Intentions:

When updating a record, hopefully it has not been updated by someone else

Optimistic lock implementation:

  • When the record is retrieved, the current version is obtained
  • Bring this version with you when you update
  • To perform an update, set version = newVersion where version = oldVersion
  • If the version is incorrect, the update fails
Update - A threaduser set name ="Dong", version= version+1 where id = 2 and version = 1- The B thread finishes first= 2, which will cause A to fail to modify! updateuser set name ="Dong", version= version+1 where id = 2 and version = 1
Copy the code

3. Configure optimistic locking

New database field ==version==

== Added the version field == to the Java entity class

@ Version:

  • Support the type of data is only: int, Integer, long, long, the Date and Timestamp, LocalDateTime
  • Integer typenewVersion = oldVersion + 1
  • newVersionWill be back to writeentity 中
  • Only supportupdateById(id) 与 update(entity, wrapper)methods
  • inupdate(entity, wrapper)Under way,wrapperCan not be reused!!

== Create configuration class and configure optimistic locking plug-in ==

@Configuration
public class MyConfig {

    //Optimistic Locking plugin@Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        returninterceptor; }}Copy the code

4. Test optimistic locking

Normally, the final result would be test 1, but the result is test 2, because of the optimistic locking mechanism, thread 1 and thread 2 initially obtain the version value of 1, but after the update of thread 2, the version is increased to 2, at this time, the update of thread 1 is not expected to be 1, so the update fails!!





The last

I am aCode pipi shrimpI am a mantis shrimp lover who loves to share knowledge. I will keep updating my blog in the future. I look forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == a key three! ==, thanks for your support, see you next time ~~~