MyBatisPlus (twenty-one) optimistic lock

scenes to be used

Used when multiple users modify the same piece of data at the same time, only one modification is allowed to be successful.

Implementation principle

uses a field to record the version of the data.
When modifying data, it will detect whether the current version is the version being modified. At the same time, it will 版本号 + 1 be modified successfully.

Method to realize

  1. Configure plugin
  2. Add@Versionannotation to the fields of the entity class

Insert image description here

code

Configure plugin

package com.example.core.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.example.web")
public class MybatisPlusConfig {
    
    

    /**
     * 添加拦截器
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
    
    
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 乐观锁插件
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); // 针对 update 和 delete 语句 作用: 阻止恶意的全表更新删除
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));// 如果配置多个插件,切记分页最后添加
        // interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
        return interceptor;
    }
}

Add@Versionannotation to the fields of the entity class

package com.example.web.entity;

import com.baomidou.mybatisplus.annotation.Version;
import lombok.Data;

@Data

public class User {
    
    

    // 其他字段

    /**
     * 版本
     */
    @Version
    private Integer version;
}

Database Table

Insert image description here

test

code

    /**
     * 插入用户
     */
    @Test
    public void insert() {
    
    
        User user = new User();
        user.setId(16L);
        user.setName("郑一");
        user.setAge(30);
        user.setEmail("[email protected]");
        user.setGender(GenderEnum.MALE);

        mapper.insert(user);
    }


    /**
     * 更新用户:版本号为空,乐观锁失效。
     */
    @Test
    public void update() {
    
    
        User user = new User();
        user.setId(16L);
        user.setAge(31);

        mapper.updateById(user);
    }


    /**
     * 更新用户:版本号 +1(版本号不为空,乐观锁有效)。
     */
    @Test
    public void updateWithVersion() {
    
    
        User user = mapper.selectById(16L);
        user.setAge(31);

        mapper.updateById(user);
    }


    /**
     * 更新用户:测试同时更新,第二个更新失败。
     */
    @Test
    public void updateConcurrent() {
    
    
        // 同步查询
        User user1 = mapper.selectById(16L);
        user1.setAge(32);

        User user2 = mapper.selectById(16L);
        user2.setAge(33);

        // 更新
        mapper.updateById(user1);
        mapper.updateById(user2);
    }


Newly inserted data

Insert image description here

The version number is empty when updating, and optimistic locking fails.

This feels like a loophole.
Insert image description here

When updating, the version number is not empty and optimistic locking is valid.

When the entity's version field不为空, optimistic locking can take effect normally.
Insert image description here

Simultaneous updates

The same piece of data is queried twice. Then two updates are called, the first update succeeds and the second update fails.

Query data twice

Insert image description here

Updated twice, the first time was successful, Updates: 1; the second time failed, Updates: 0.

Insert image description here

Guess you like

Origin blog.csdn.net/sgx1825192/article/details/133935594