马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
悲观锁的应用场景
悲观锁的根本头脑是假设并发辩论会发生,因此在操纵数据时会先锁定命据,直到完成操纵并提交事件后才开释锁。这种方式实用于写操纵较多、并发辩论大概性较高的场景。
高写入比例的数据库操纵:如果体系中有许多写操纵,而且这些写操纵大概会频仍地相互干扰,那么使用悲观锁可以有用制止数据不划一的标题。
对数据划一性要求高的场景:比如金融生意业务体系,银行转账,高并发点赞等,必要确保在任何时间的数据都是划一的,不答应出现脏读、不可重复读等标题。
乐观锁的应用场景
乐观锁则假定并发辩论不会常常发生,因此它不会在开始操纵时就锁定资源,而是在提交更新时查抄是否有其他事件已经修改了该数据。如果检测到辩论,则拒绝此次操纵。乐观锁更实用于读多写少的环境。
读操纵远多于写操纵的场景:比方在线阅读平台、消息网站等,这类应用重要以读取信息为主,很少会有数据修改的需求,采取乐观锁可以镌汰锁带来的性能斲丧。
低辩论概率的环境:当体系预期差别事件之间很少会对同一数据举行修改时,使用乐观锁可以得到更好的性能表现。比如库存管理体系中,对于非热销商品的库存调解。
选择哪种锁机制应基于详细的应用场景以及体系对性能和划一性的需求来决定。在实际开发过程中,还必要思量死锁防备、锁的粒度等因素。
必要留意的是select ** for update 是数据库层面的锁,出现标题不好排查,发起使用分布式锁。
点赞悲观锁实现
以下是基于 Java 和 MyBatis-Plus 实现悲观锁的点赞功能的完备代码示例。
1. 数据库表计划
假设我们有一个 articles 表,包罗以下字段:
id: 文章唯一标识。
title: 文章标题。
content: 文章内容。
likes: 点赞数。
sql
深色版本
CREATE TABLE articles (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
likes INT DEFAULT 0
);
2. 设置 MyBatis-Plus 和数据库毗连
2.1 添加依赖
在 pom.xml 文件中添加 MyBatis-Plus 和 MySQL 驱动依赖(与之前划一)。
2.2 设置数据库毗连
在 application.yml 中设置数据库毗连信息(与之前划一)。
3. 实体类和 Mapper 接口
3.1 创建实体类
java
深色版本- import com.baomidou.mybatisplus.annotation.TableId;
- import com.baomidou.mybatisplus.annotation.TableName;
- import lombok.Data;
- @Data
- @TableName("articles")
- public class Article {
- @TableId
- private Long id;
- private String title;
- private String content;
- private Integer likes;
- }
复制代码 3.2 创建 Mapper 接口
java
深色版本- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
- import org.apache.ibatis.annotations.Update;
- @Mapper
- public interface ArticleMapper extends BaseMapper<Article> {
- /**
- * 悲观锁查询文章
- */
- @Select("SELECT * FROM articles WHERE id = #{id} FOR UPDATE")
- Article selectForUpdate(Long id);
- /**
- * 更新点赞数
- */
- @Update("UPDATE articles SET likes = likes + 1 WHERE id = #{id}")
- int incrementLikes(Long id);
- }
复制代码 4. 实现点赞功能
4.1 Service 层实现
悲观锁的核心在于使用 FOR UPDATE 锁定命据行,确保在事件提交前其他事件无法修改该行数据。
java
深色版本- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- @Service
- public class ArticleService {
- @Autowired
- private ArticleMapper articleMapper;
- /**
- * 使用悲观锁实现点赞
- */
- @Transactional // 开启事务
- public String likeArticle(Long articleId) {
- // 使用悲观锁查询文章(锁定该行)
- Article article = articleMapper.selectForUpdate(articleId);
- if (article == null) {
- return "Article not found";
- }
- // 更新点赞数
- int rows = articleMapper.incrementLikes(articleId);
- if (rows > 0) {
- return "Like successful, new likes: " + (article.getLikes() + 1);
- } else {
- return "Failed to update likes";
- }
- }
- }
复制代码 4.2 Controller 层实现
java
深色版本- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- @RestController
- @RequestMapping("/articles")
- public class ArticleController {
- @Autowired
- private ArticleService articleService;
- /**
- * 点赞接口
- */
- @PostMapping("/{id}/like")
- public String likeArticle(@PathVariable Long id) {
- return articleService.likeArticle(id);
- }
- }
复制代码 5. 测试接口
启动 Spring Boot 应用后,可以通过以下方式测试点赞功能:
哀求示例
bash
深色版本
POST http://localhost:8080/articles/1/like
相应结果
乐成时返回:“Like successful, new likes: X”
如果文章不存在,返回:“Article not found”
6. 核心原理
悲观锁的工作流程:
在查询文章时,使用 FOR UPDATE 锁定目的数据行。
在事件提交之前,其他事件无法对该行数据举行修改。
更新点赞数后提交事件,开释锁。
SQL 示例:
sql
深色版本
– 查询并锁定命据行
SELECT * FROM articles WHERE id = 1 FOR UPDATE;
– 更新点赞数
UPDATE articles SET likes = likes + 1 WHERE id = 1;
实用场景:
实用于写操纵较多且并发辩论概率较高的场景。
比方,在高并发环境下对同一篇文章举行频仍点赞时,可以制止数据不划一的标题。
7. 留意事项
性能开销:
悲观锁会锁定命据行,大概导致其他事件壅闭,影响体系性能。
因此,只有在确实必要严格包管数据划一性时才使用悲观锁。
死锁风险:
如果多个事件同时实行锁定差别的资源,大概会导致死锁。
必要公道计划事件逻辑,只管镌汰死锁的大概性。
事件管理:
必须确保在事件中实行锁定和更新操纵,否则锁定不起作用。
通过以上代码,我们可以使用悲观锁实现点赞功能,确保在高并发场景下的数据划一性。
点赞乐观锁实现
以下是使用 Java 和 MyBatis-Plus 实现乐观锁的代码示例。我们将基于 Spring Boot 和 MyBatis-Plus 框架完成点赞功能。
1. 数据库表计划
假设我们有一个 articles 表,包罗以下字段:
id: 文章唯一标识。
title: 文章标题。
content: 文章内容。
likes: 点赞数。
version: 版本号(用于乐观锁)。
sql
深色版本
CREATE TABLE articles (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
likes INT DEFAULT 0,
version INT DEFAULT 0
);
2. 设置 MyBatis-Plus 和数据库毗连
2.1 添加依赖
在 pom.xml 文件中添加 MyBatis-Plus 和 MySQL 驱动依赖:
xml
深色版本
org.springframework.boot
spring-boot-starter-web
- <!-- MyBatis-Plus -->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.5.3.1</version>
- </dependency>
- <!-- MySQL Driver -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <!-- Lombok (Optional for code simplification) -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
复制代码 2.2 设置数据库毗连在 application.yml 中设置数据库毗连信息:yaml
深色版本
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: your_username
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
configuration:
map-underscore-to-camel-case: true
3. 实体类和 Mapper 接口
3.1 创建实体类
使用 @Version 注解来标识版本号字段。
java
深色版本- import com.baomidou.mybatisplus.annotation.TableField;
- import com.baomidou.mybatisplus.annotation.TableId;
- import com.baomidou.mybatisplus.annotation.TableName;
- import com.baomidou.mybatisplus.annotation.Version;
- import lombok.Data;
- @Data
- @TableName("articles")
- public class Article {
- @TableId
- private Long id;
- private String title;
- private String content;
- private Integer likes;
- @Version
- private Integer version; // 版本号字段,用于乐观锁
- }
复制代码 3.2 创建 Mapper 接口
继续 MyBatis-Plus 提供的 BaseMapper 接口。
java
深色版本- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import org.apache.ibatis.annotations.Mapper;
- @Mapper
- public interface ArticleMapper extends BaseMapper<Article> {
- }
复制代码 4. 设置乐观锁插件
在 Spring Boot 启动类中设置 MyBatis-Plus 的乐观锁插件。
java
深色版本- import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
- import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class MyBatisPlusConfig {
- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor() {
- MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
- // 添加乐观锁插件
- interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
- return interceptor;
- }
- }
复制代码 5. 实现点赞功能
5.1 Service 层实现
编写点赞逻辑,使用乐观锁更新点赞数。
java
深色版本- import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- @Service
- public class ArticleService {
- @Autowired
- private ArticleMapper articleMapper;
- public String likeArticle(Long articleId) {
- // 查询文章信息
- Article article = articleMapper.selectById(articleId);
- if (article == null) {
- return "Article not found";
- }
- // 更新点赞数和版本号
- article.setLikes(article.getLikes() + 1);
- // 使用 MyBatis-Plus 自动处理乐观锁
- int rows = articleMapper.updateById(article);
- if (rows > 0) {
- return "Like successful, new likes: " + article.getLikes();
- } else {
- return "Optimistic lock conflict, please try again";
- }
- }
- }
复制代码 5.2 Controller 层实现
提供 RESTful API 接口。
java
深色版本- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- @RestController
- @RequestMapping("/articles")
- public class ArticleController {
- @Autowired
- private ArticleService articleService;
- @PostMapping("/{id}/like")
- public String likeArticle(@PathVariable Long id) {
- return articleService.likeArticle(id);
- }
- }
复制代码 6. 测试接口
启动 Spring Boot 应用后,可以通过以下方式测试点赞功能:
哀求示例
bash
深色版本
POST http://localhost:8080/articles/1/like
相应结果
乐成时返回:“Like successful, new likes: X”
如果发生乐观锁辩论,返回:“Optimistic lock conflict, please try again”
7. 核心原理
乐观锁的核心头脑:
在更新数据时,MyBatis-Plus 会主动查抄 version 字段是否匹配。
如果匹配,则更新数据并递增 version。
如果不匹配,则更新失败,返回受影响行数为 0。
SQL 示例:
sql
深色版本
UPDATE articles
SET likes = likes + 1, version = version + 1
WHERE id = 1 AND version = current_version;
实用场景:
实用于读多写少且辩论概率较低的场景,如点赞、品评、欣赏量统计等。
通过以上代码,我们可以轻松实现基于 MyBatis-Plus 的乐观锁功能,确保在高并发场景下的数据划一性。
银行转账悲观锁实现
在银行转账的场景中,悲观锁是一种非常符合的并发控制机制。由于银行转账涉及资金的运动,数据划一性至关紧张,因此使用悲观锁可以确保在事件完成之前,干系账户不会被其他事件修改。
以下是基于 Java 和 MyBatis-Plus 实现银行转账功能的完备代码示例,此中使用了悲观锁来包管事件的安全性。
1. 数据库表计划
假设我们有一个 accounts 表,用于存储用户账户信息:
id: 账户唯一标识。
user_name: 用户名。
balance: 账户余额。
sql
深色版本
CREATE TABLE accounts (
id INT PRIMARY KEY AUTO_INCREMENT,
user_name VARCHAR(255) NOT NULL,
balance DECIMAL(10, 2) NOT NULL DEFAULT 0.00
);
2. 设置 MyBatis-Plus 和数据库毗连
2.1 添加依赖
在 pom.xml 文件中添加 MyBatis-Plus 和 MySQL 驱动依赖(与之前划一)。
2.2 设置数据库毗连
在 application.yml 中设置数据库毗连信息:
yaml
深色版本
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: your_username
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
configuration:
map-underscore-to-camel-case: true
3. 实体类和 Mapper 接口
3.1 创建实体类
java
深色版本- import com.baomidou.mybatisplus.annotation.TableId;
- import com.baomidou.mybatisplus.annotation.TableName;
- import lombok.Data;
- import java.math.BigDecimal;
- @Data
- @TableName("accounts")
- public class Account {
- @TableId
- private Long id;
- private String userName;
- private BigDecimal balance;
- }
复制代码 3.2 创建 Mapper 接口
java
深色版本- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
- import org.apache.ibatis.annotations.Update;
- @Mapper
- public interface AccountMapper extends BaseMapper<Account> {
- /**
- * 悲观锁查询账户
- */
- @Select("SELECT * FROM accounts WHERE id = #{id} FOR UPDATE")
- Account selectForUpdate(Long id);
- /**
- * 更新账户余额
- */
- @Update("UPDATE accounts SET balance = balance + #{amount} WHERE id = #{id}")
- int updateBalance(Long id, BigDecimal amount);
- }
复制代码 4. 实现转账功能
4.1 Service 层实现
悲观锁的核心在于使用 FOR UPDATE 锁定账户行,确保在事件提交前其他事件无法修改账户余额。
java
深色版本- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.math.BigDecimal;
- @Service
- public class AccountService {
- @Autowired
- private AccountMapper accountMapper;
- /**
- * 使用悲观锁实现转账
- *
- * @param fromAccountId 转出账户 ID
- * @param toAccountId 转入账户 ID
- * @param amount 转账金额
- */
- @Transactional // 开启事务
- public String transfer(Long fromAccountId, Long toAccountId, BigDecimal amount) {
- if (amount.compareTo(BigDecimal.ZERO) <= 0) {
- return "Invalid transfer amount";
- }
- // 查询转出账户并加锁
- Account fromAccount = accountMapper.selectForUpdate(fromAccountId);
- if (fromAccount == null || fromAccount.getBalance().compareTo(amount) < 0) {
- return "Insufficient balance or invalid account";
- }
- // 查询转入账户并加锁
- Account toAccount = accountMapper.selectForUpdate(toAccountId);
- if (toAccount == null) {
- return "Invalid target account";
- }
- // 执行转账操作
- accountMapper.updateBalance(fromAccountId, amount.negate()); // 减少转出账户余额
- accountMapper.updateBalance(toAccountId, amount); // 增加转入账户余额
- return "Transfer successful";
- }
- }
复制代码 4.2 Controller 层实现
java
深色版本- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import java.math.BigDecimal;
- @RestController
- @RequestMapping("/accounts")
- public class AccountController {
- @Autowired
- private AccountService accountService;
- /**
- * 转账接口
- */
- @PostMapping("/transfer")
- public String transfer(
- @RequestParam Long fromAccountId,
- @RequestParam Long toAccountId,
- @RequestParam BigDecimal amount) {
- return accountService.transfer(fromAccountId, toAccountId, amount);
- }
- }
复制代码 5. 测试接口
启动 Spring Boot 应用后,可以通过以下方式测试转账功能:
哀求示例
bash
深色版本
POST http://localhost:8080/accounts/transfer?fromAccountId=1&toAccountId=2&amount=100.00
相应结果
乐成时返回:“Transfer successful”
如果余额不敷或账户无效,返回相应的错误信息。
6. 核心原理
悲观锁的工作流程:
在查询账户时,使用 FOR UPDATE 锁定目的账户的数据行。
在事件提交之前,其他事件无法对该账户举行修改。
完成转账操纵后提交事件,开释锁。
SQL 示例:
sql
深色版本
– 查询并锁定账户
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
– 更新账户余额
UPDATE accounts SET balance = balance - 100.00 WHERE id = 1;
UPDATE accounts SET balance = balance + 100.00 WHERE id = 2;
实用场景:
实用于写操纵较多且并发辩论概率较高的场景。
比方,在高并发环境下举行银行转账时,可以制止重复扣款或余额不划一的标题。
7. 留意事项
性能开销:
悲观锁会锁定命据行,大概导致其他事件壅闭,影响体系性能。
因此,只有在确实必要严格包管数据划一性时才使用悲观锁。
死锁风险:
如果多个事件同时实行锁定差别的资源,大概会导致死锁。
必要公道计划事件逻辑,只管镌汰死锁的大概性(比方,按照固定的次序锁定账户)。
事件管理:
必须确保在事件中实行锁定和更新操纵,否则锁定不起作用。
通过以上代码,我们可以使用悲观锁实现银行转账功能,确保在高并发场景下的数据划一性。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |