MyBatisPlus+SpringBoot實現(xiàn)樂觀鎖功能詳細流程
一、商城數(shù)據(jù)不一致的場景
如果商城中有一件商品,成本價是80元,售價是100元。經(jīng)理先是通知小李,說你去把商品價格增加50元。小李正在玩游戲,耽擱了一個小時。正好一個小時后,經(jīng)理覺得商品價格增加到150元,價格太高,可能會影響銷量。又通知小王,你把商品價格降低30元。
此時,小李和小王同時操作商品后臺系統(tǒng)。小李操作的時候,系統(tǒng)先取出商品價格100元;小王也在操作,取出的商品價格也是100元。小李將價格加了50元,并將100+50=150元存入了數(shù)據(jù)庫;小王將商品減了30元,并將100-30=70元存入了數(shù)據(jù)庫。是的,如果沒有鎖,小李的操作就完全被小王的覆蓋了。
現(xiàn)在商品價格是70元,比成本價低10元。幾分鐘后,這個商品很快出售了1千多件商品,老板虧1萬多。
二、演示這一過程
1、數(shù)據(jù)庫中增加商品表
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 '價格',
version INT(11) DEFAULT 0 COMMENT '樂觀鎖版本號',
PRIMARY KEY (id)
);
INSERT INTO product (id, NAME, price) VALUES (1, '筆記本', 100);
2、創(chuàng)建實體類
@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、進行測試
@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、小李將價格加了50元,存入了數(shù)據(jù)庫
p1.setPrice(p1.getPrice() + 50);
int result1 = productMapper.updateById(p1);
System.out.println("小李修改結(jié)果:" + result1);
//4、小王將商品減了30元,存入了數(shù)據(jù)庫
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ā)生,解決方案是使用樂觀鎖
三、樂觀鎖方案
數(shù)據(jù)庫中添加version字段:取出記錄時,獲取當前version
SELECT id,`name`,price,`version` FROM product WHERE id=1
更新時,version + 1,如果where語句中的version版本不對,則更新失敗
UPDATE product SET price=price+50, `version`=`version` + 1 WHERE id=1 AND `version`=1
四、樂觀鎖實現(xiàn)流程
1、修改實體類
添加 @Version 注解
@Version private Integer version;
2、添加樂觀鎖插件
@Configuration
//@MapperScan("com.koo.modules.*.dao")
public class MybatisPlusConfig {
/**
* 新的分頁插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問題(該屬性會在舊插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//實現(xiàn)樂觀鎖,保證數(shù)據(jù)的準確性
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
3、優(yōu)化流程
(判斷第二次更新數(shù)據(jù)是否成功,不成功則重新取數(shù)據(jù)進行更新)
@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、小李將價格加了50元,存入了數(shù)據(jù)庫
p1.setPrice(p1.getPrice() + 50);
int result1 = productMapper.updateById(p1);
System.out.println("小李修改結(jié)果:" + result1);
//4、小王將商品減了30元,存入了數(shù)據(jù)庫
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ù)正確
至此,一個簡單的樂觀鎖就實現(xiàn)了。
總結(jié)
到此這篇關(guān)于MyBatisPlus+SpringBoot實現(xiàn)樂觀鎖功能的文章就介紹到這了,更多相關(guān)MyBatisPlus SpringBoot樂觀鎖功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ?IDEA?2023版本創(chuàng)建Spring項目時Java只能選擇17或21的問題解決方法
spring-boot是一個基于Java的開源框架,用于快速構(gòu)建生產(chǎn)級別的應(yīng)用程序,這篇文章主要給大家介紹了關(guān)于IntelliJ?IDEA?2023版本創(chuàng)建Spring項目時Java只能選擇17或21的問題解決方法,需要的朋友可以參考下2024-07-07
Java Testcontainers庫實現(xiàn)測試功能
這篇文章主要介紹了Java Testcontainers庫實現(xiàn)測試功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2020-09-09
Java C++解決在排序數(shù)組中查找數(shù)字出現(xiàn)次數(shù)問題
本文終于介紹了分別通過Java和C++實現(xiàn)統(tǒng)計一個數(shù)字在排序數(shù)組中出現(xiàn)的次數(shù)。文中詳細介紹了實現(xiàn)思路,感興趣的小伙伴可以跟隨小編學(xué)習一下2021-12-12
Java concurrency之鎖_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細介紹了Java concurrency之鎖的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
關(guān)于java開發(fā)的性能問題總結(jié)(必看)
下面小編就為大家?guī)硪黄P(guān)于java開發(fā)的性能問題總結(jié)(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
Maven分步詳解多環(huán)境配置與應(yīng)用流程
這篇文章主要介紹了Maven進階多環(huán)境配置與應(yīng)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-08-08

