搜点济南网站建设,医院推广营销方式,软件详细设计文档模板,小程序代注册乐观锁和悲观锁
场景
一件商品#xff0c;成本价是80元#xff0c;售价是100元。老板先是通知小李#xff0c;说你去把商品价格增加50元。小李正在玩游戏#xff0c;耽搁了一个小时。正好一个小时后#xff0c;老板觉得商品价格增加到150元#xff0c;价格太高#xf…乐观锁和悲观锁
场景
一件商品成本价是80元售价是100元。老板先是通知小李说你去把商品价格增加50元。小李正在玩游戏耽搁了一个小时。正好一个小时后老板觉得商品价格增加到150元价格太高可能会影响销量。又通知小王你把商品价格降低30元。
此时小李和小王同时操作商品后台系统。小李操作的时候系统先取出商品价格100元小王也在操作取出的商品价格也是100元。小李将价格加了50元并将10050150元存入了数据库小王将商品减了30元并将100-3070元存入了数据库。是的如果没有锁小李的操作就完全被小王的覆盖了。
现在商品价格是70元比成本价低10元。几分钟后这个商品很快出售了1千多件商品老板亏1万多。
上面的故事如果是乐观锁小王保存价格前会检查下价格是否被人修改过了。如果被修改过了则重新取出的被修改后的价格150元这样他会将120元存入数据库。
如果是悲观锁小李取出数据后小王只能等小李操作完之后才能对价格进行操作也会保证最终的价格是120元。
模拟修改冲突
数据库中增加商品表
CREATE TABLE t_product (id BIGINT(20) NOT NULL COMMENT 主键ID, NAME VARCHAR(30) NULL DEFAULT NULL COMMENT 商品名称, price INT(11) DEFAULT 0 COMMENT 价格, VERSION INT(11) DEFAULT 0 COMMENT 乐观锁版本号, PRIMARY KEY (id) );添加数据
INSERT INTO t_product (id, NAME, price) VALUES (1, 外星人笔记本, 100);添加实体
package com.xxxx.mybatisplus.pojo;import lombok.Data;Data
public class Product {private Long id;private String name;private Integer price;private Integer version;
}
ProductMapper
package com.xxxx.mybatisplus.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xxxx.mybatisplus.pojo.Product;
import org.springframework.stereotype.Repository;Repository
public interface ProductMapper extends BaseMapperProduct {}
测试
package com.xxxx.mybatisplus;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxxx.mybatisplus.mapper.ProductMapper;
import com.xxxx.mybatisplus.mapper.UserMapper;
import com.xxxx.mybatisplus.pojo.Product;
import com.xxxx.mybatisplus.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;SpringBootTest
public class MyBatisPlusPluginsTest {Autowiredprivate UserMapper userMapper;Autowiredprivate ProductMapper productMapper;Testpublic void testProduct01(){// 小李查询商品价格Product productLi productMapper.selectById(1);System.out.println(小李查询的商品价格 productLi.getPrice());// 100// 小王查询商品价格Product productWang productMapper.selectById(1);System.out.println(小王查询的商品价格 productWang.getPrice());// 100// 小李将商品价格50productLi.setPrice(productLi.getPrice() 50);productMapper.updateById(productLi);// 小王将商品价格-30productWang.setPrice(productWang.getPrice()-30);productMapper.updateById(productWang);// 老板查询商品价格Product productBoss productMapper.selectById(1);System.out.println(老板查询的商品价格 productBoss.getPrice());// 70}}
乐观锁实现流程
数据库中添加version字段
取出记录时获取当前version
SELECT id,name,price,version FROM product WHERE id1更新时version 1如果where语句中的version版本不对则更新失败
UPDATE product SET priceprice50, versionversion 1 WHERE id1 AND version1Mybatis-Plus实现乐观锁
修改实体类 修改配置类 测试 优化修改流程 Testpublic void testProduct01(){// 小李查询商品价格Product productLi productMapper.selectById(1);System.out.println(小李查询的商品价格 productLi.getPrice());// 100// 小王查询商品价格Product productWang productMapper.selectById(1);System.out.println(小王查询的商品价格 productWang.getPrice());// 100// 小李将商品价格50productLi.setPrice(productLi.getPrice() 50);productMapper.updateById(productLi);// 小王将商品价格-30productWang.setPrice(productWang.getPrice()-30);int result productMapper.updateById(productWang);if (result 0){// 操作失败,重试Product productNew productMapper.selectById(1);productNew.setPrice(productNew.getPrice() - 30);productMapper.updateById(productNew);}// 老板查询商品价格Product productBoss productMapper.selectById(1);System.out.println(老板查询的商品价格 productBoss.getPrice());// 120}