MyBatisPlus+SpringBoot實(shí)現(xiàn)樂(lè)觀鎖功能詳細(xì)流程
一、商城數(shù)據(jù)不一致的場(chǎng)景
如果商城中有一件商品,成本價(jià)是80元,售價(jià)是100元。經(jīng)理先是通知小李,說(shuō)你去把商品價(jià)格增加50元。小李正在玩游戲,耽擱了一個(gè)小時(shí)。正好一個(gè)小時(shí)后,經(jīng)理覺(jué)得商品價(jià)格增加到150元,價(jià)格太高,可能會(huì)影響銷量。又通知小王,你把商品價(jià)格降低30元。
此時(shí),小李和小王同時(shí)操作商品后臺(tái)系統(tǒng)。小李操作的時(shí)候,系統(tǒng)先取出商品價(jià)格100元;小王也在操作,取出的商品價(jià)格也是100元。小李將價(jià)格加了50元,并將100+50=150元存入了數(shù)據(jù)庫(kù);小王將商品減了30元,并將100-30=70元存入了數(shù)據(jù)庫(kù)。是的,如果沒(méi)有鎖,小李的操作就完全被小王的覆蓋了。
現(xiàn)在商品價(jià)格是70元,比成本價(jià)低10元。幾分鐘后,這個(gè)商品很快出售了1千多件商品,老板虧1萬(wàn)多。
二、演示這一過(guò)程
1、數(shù)據(jù)庫(kù)中增加商品表
CREATE TABLE product
(
id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名稱',
price INT(11) DEFAULT 0 COMMENT '價(jià)格',
version INT(11) DEFAULT 0 COMMENT '樂(lè)觀鎖版本號(hào)',
PRIMARY KEY (id)
);
INSERT INTO product (id, NAME, price) VALUES (1, '筆記本', 100);
2、創(chuàng)建實(shí)體類
@Data
public class Product {
private Long id;
private String name;
private Integer price;
private Integer version;
}
3、創(chuàng)建Mapper
public interface ProductMapper extends BaseMapper<Product> {
}
4、進(jìn)行測(cè)試
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductVersionTest {
@Resource
private ProductMapper productMapper;
@Test
public void testProductUpdate() {
//1、小李
Product p1 = productMapper.selectById(1L);
//2、小王
Product p2 = productMapper.selectById(1L);
//3、小李將價(jià)格加了50元,存入了數(shù)據(jù)庫(kù)
p1.setPrice(p1.getPrice() + 50);
int result1 = productMapper.updateById(p1);
System.out.println("小李修改結(jié)果:" + result1);
//4、小王將商品減了30元,存入了數(shù)據(jù)庫(kù)
p2.setPrice(p2.getPrice() - 30);
int result2 = productMapper.updateById(p2);
System.out.println("小王修改結(jié)果:" + result2);
//最后的結(jié)果
Product p3 = productMapper.selectById(1L);
System.out.println("最后的結(jié)果:" + p3.getPrice());
}
}
最后輸出的是 70元,與經(jīng)理預(yù)期的120元不同,導(dǎo)致虧損,如何防止這樣的異常發(fā)生,解決方案是使用樂(lè)觀鎖
三、樂(lè)觀鎖方案
數(shù)據(jù)庫(kù)中添加version字段:取出記錄時(shí),獲取當(dāng)前version
SELECT id,`name`,price,`version` FROM product WHERE id=1
更新時(shí),version + 1,如果where語(yǔ)句中的version版本不對(duì),則更新失敗
UPDATE product SET price=price+50, `version`=`version` + 1 WHERE id=1 AND `version`=1
四、樂(lè)觀鎖實(shí)現(xiàn)流程
1、修改實(shí)體類
添加 @Version 注解
@Version private Integer version;
2、添加樂(lè)觀鎖插件
@Configuration
//@MapperScan("com.koo.modules.*.dao")
public class MybatisPlusConfig {
/**
* 新的分頁(yè)插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問(wèn)題(該屬性會(huì)在舊插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//實(shí)現(xiàn)樂(lè)觀鎖,保證數(shù)據(jù)的準(zhǔn)確性
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
3、優(yōu)化流程
(判斷第二次更新數(shù)據(jù)是否成功,不成功則重新取數(shù)據(jù)進(jìn)行更新)
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductVersionTest {
@Resource
private ProductMapper productMapper;
@Test
public void testProductUpdate() {
//1、小李
Product p1 = productMapper.selectById(1L);
//2、小王
Product p2 = productMapper.selectById(1L);
//3、小李將價(jià)格加了50元,存入了數(shù)據(jù)庫(kù)
p1.setPrice(p1.getPrice() + 50);
int result1 = productMapper.updateById(p1);
System.out.println("小李修改結(jié)果:" + result1);
//4、小王將商品減了30元,存入了數(shù)據(jù)庫(kù)
p2.setPrice(p2.getPrice() - 30);
int result2 = productMapper.updateById(p2);
System.out.println("小王修改結(jié)果:" + result2);
if(result2 == 0){//更新失敗,重試
System.out.println("小王重試");
//重新獲取數(shù)據(jù)
p2 = productMapper.selectById(1L);
//更新
p2.setPrice(p2.getPrice() - 30);
productMapper.updateById(p2);
}
//最后的結(jié)果
Product p3 = productMapper.selectById(1L);
System.out.println("最后的結(jié)果:" + p3.getPrice());
}
}
輸出結(jié)果為120,數(shù)據(jù)正確
至此,一個(gè)簡(jiǎn)單的樂(lè)觀鎖就實(shí)現(xiàn)了。
總結(jié)
到此這篇關(guān)于MyBatisPlus+SpringBoot實(shí)現(xiàn)樂(lè)觀鎖功能的文章就介紹到這了,更多相關(guān)MyBatisPlus SpringBoot樂(lè)觀鎖功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決SpringMVC 返回Java8 時(shí)間JSON數(shù)據(jù)的格式化問(wèn)題處理
本篇文章主要介紹了解決SpringMVC 返回Java8 時(shí)間JSON數(shù)據(jù)的格式化問(wèn)題處理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
SpringBatch數(shù)據(jù)寫(xiě)入實(shí)現(xiàn)
Spring Batch通過(guò)ItemWriter接口及其豐富的實(shí)現(xiàn),提供了強(qiáng)大的數(shù)據(jù)寫(xiě)入能力,本文主要介紹了SpringBatch數(shù)據(jù)寫(xiě)入實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04
java實(shí)現(xiàn)兩個(gè)文件的異或運(yùn)算
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)兩個(gè)文件的異或運(yùn)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
SpringBoot集成cache緩存的實(shí)現(xiàn)
日常開(kāi)發(fā)中,緩存是解決數(shù)據(jù)庫(kù)壓力的一種方案,本文記錄springboot中使用cache緩存。需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
如何在Springboot實(shí)現(xiàn)攔截器功能
其實(shí)spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了,下面這篇文章主要給大家介紹了關(guān)于如何在Springboot實(shí)現(xiàn)攔截器功能的相關(guān)資料,需要的朋友可以參考下2022-06-06
Java中如何計(jì)算一段程序的運(yùn)行時(shí)間
這篇文章主要介紹了Java中如何計(jì)算一段程序的運(yùn)行時(shí)間問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

