java 乐观锁与悲观锁的使用场景 - math
{
}
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{
/**
* 悲观锁查询账户
*/
@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. 注意事项
性能开销: 悲观锁会锁定数据行,可能导致其他事务阻塞,影响系统性能。 因此,只有在确实需要严格保证数据一致性时才使用悲观锁。 死锁风险: 如果多个事务同时尝试锁定不同的资源,可能会导致死锁。 需要合理设计事务逻辑,尽量减少死锁的可能性(例如,按照固定的顺序锁定账户)。 事务管理: 必须确保在事务中执行锁定和更新操作,否则锁定不起作用。 通过以上代码,我们可以利用悲观锁实现银行转账功能,确保在高并发场景下的数据一致性。
java 乐观锁与悲观锁的使用场景
好文推荐