如何通过MyBatis实现乐观锁机制,有效应对并发冲突?

2026-05-21 09:593阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1589个文字,预计阅读时间需要7分钟。

如何通过MyBatis实现乐观锁机制,有效应对并发冲突?

场景展示:银行两操作员同时操作同一账户即是典型例子。例如,A、B操作员同时读取金额为1000元的账户,A操作员为该账户增加100元,B操作员为该账户扣除50元,先提交A后提交B。

情景展示:

银行两操作员同时操作同一账户就是典型的例子。

比如A、B操作员同时读取一余额为1000元的账户,A操作员为该账户增加100元,B操作员同时为该账户扣除50元,A先提交,B后提交。最后实际账户余额为1000-50=950元,但本该为1000+100-50=1050。这就是典型的并发问题。

乐观锁机制在一定程度上解决了这个问题。乐观锁,大多是基于数据版本(Version)记录机制实现。何谓数据版本?即为数据增加一个版本标识,在基于数据库表的版本解决方案中,一般是通过为数据库表增加一个 “version” 字段来实现。

读取出数据时,将此版本号一同读出,之后更新时,对此版本号加一。此时,将提交数据的版本数据与数据库表对应记录的当前版本信息进行比对,如果提交的数据版本号大于数据库表当前版本号,则予以更新,否则认为是过期数据。

如何通过MyBatis实现乐观锁机制,有效应对并发冲突?

对于上面修改用户帐户信息的例子而言,假设数据库中帐户信息表中有一个version字段,当前值为1;而当前帐户余额字段(balance)为1000元。假设操作员A先更新完,操作员B后更新。

a、操作员A此时将其读出(version=1),并从其帐户余额中增加100(1000+100=1100)。

b、在操作员A操作的过程中,操作员B也读入此用户信息(version=1),并从其帐户余额中扣除50(1000-50=950)。

c、操作员A完成了修改工作,将数据版本号加一(version=2),连同帐户增加后余额(balance=1100),提交至数据库更新,此时由于提交数据版本大于数据库记录当前版本,数据被更新,数据库记录version更新为2。

d、操作员B完成了操作,也将版本号加一(version=2)试图向数据库提交数据(balance=950),但此时比对数据库记录版本时发现,操作员B提交的数据版本号为2,数据库记录当前版本也为2,不满足 “提交版本必须大于记录当前版本才能执行更新 “的乐观锁策略,因此,操作员B的提交被驳回。

这样,就避免了操作员B用基于version=1的旧数据修改的结果覆盖操作员A的操作结果的可能。

示例代码:

account建库脚本

drop table if exists account_wallet; /*==============================================================*/ /* Table: account_wallet */ /*==============================================================*/ create table account_wallet ( id int not null comment '用户钱包主键', user_open_id varchar(64) comment '用户中心的用户唯一编号', user_amount decimal(10,5), create_time datetime, update_time datetime, pay_password varchar(64), is_open int comment '0:代表未开启支付密码,1:代表开发支付密码', check_key varchar(64) comment '平台进行用户余额更改时,首先效验key值,否则无法进行用户余额更改操作', version int comment '基于mysql乐观锁,解决并发访问' primary key (id) );

dao层

AccountWallet selectByOpenId(String openId);

int updateAccountWallet(AccountWallet record);

service 层

AccountWallet selectByOpenId(String openId);

int updateAccountWallet(AccountWallet record);

serviceImpl层

public AccountWallet selectByOpenId(String openId) { // TODO Auto-generated method stub return accountWalletMapper.selectByOpenId(openId); } public int updateAccountWallet(AccountWallet record) { // TODO Auto-generated method stub return accountWalletMapper.updateAccountWallet(record); }

sql.xml

<!--通过用户唯一编号,查询用户钱包相关的信息 --> <select id="selectByOpenId" resultMap="BaseResultMap" parameterType="java.lang.String"> select <include refid="Base_Column_List" /> from account_wallet where user_open_id = #{openId,jdbcType=VARCHAR} </select> <!--用户钱包数据更改 ,通过乐观锁(version机制)实现 --> <update id="updateAccountWallet" parameterType="com.settlement.model.AccountWallet"> <![CDATA[ update account_wallet set user_amount = #{userAmount,jdbcType=DECIMAL}, version = version + 1 where id =#{id,jdbcType=INTEGER} and version = #{version,jdbcType=INTEGER} ]]> </update>

controller 层

package com.settlement.controller; import java.math.BigDecimal; import javax.servlet.localhost:8080/Settlement/wallet/walleroptimisticlock.action", "openId="+openId+"&openType="+openType+"&amount="+amount); System.out.println("操作结果:"+result); System.out.println("模拟用户: "+workerName+" 模拟请求结束 at "+sdf.format(new Date())); } } }

补充知识:Mybatis-plus代码生成器,自用版本不带xml

package com.wuyd.mybatispulsdemo; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; /** * @author wuyd * 创建时间:2019/10/8 11:17 */ public class CodeGenerator { public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); //全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(System.getProperty("user.dir")+"/src/main/java"); gc.setFileOverride(true); //不需要ActiveRecord特性的请改为false gc.setActiveRecord(true); gc.setSwagger2(true); gc.setAuthor("wuyd"); //自定义文件命名,注意%s 会自动填充表实体属性 gc.setControllerName("%sController"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setEntityName("%sEntity"); gc.setMapperName("%sMapper"); mpg.setGlobalConfig(gc); //数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("xxx"); dsc.setPassword("xxx"); dsc.setUrl("jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxxx?useUnicode=true&useSSL=false&characterEncoding=utf8"); mpg.setDataSource(dsc); //策略配置 StrategyConfig strategy = new StrategyConfig(); //此处可以修改您的表前缀 strategy.setTablePrefix(new String[]{}); //表名生成策略 strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); //需要生成的表 strategy.setInclude(new String[]{"knapsacks","knapsacks_kind","knapsacks_prop","knapsacks_recharge_card"}); strategy.setSuperServiceClass(null); strategy.setSuperServiceImplClass(null); strategy.setSuperMapperClass(null); strategy.setControllerMappingHyphenStyle(true); strategy.setEntityLombokModel(true); strategy.setEntitySerialVersionUID(true); strategy.setEntityTableFieldAnnotationEnable(true); mpg.setStrategy(strategy); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); //包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.wuyd.mybatispulsdemo"); pc.setController("controller"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setMapper("mapper"); pc.setEntity("entity"); mpg.setPackageInfo(pc); //执行生成 mpg.execute(); } }

pom.xml

<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.2.0</version> </dependency> <!-- ORM 选一款 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus-boot-starter.version}</version> </dependency> <!-- Mysql驱动 注意版本!--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector.version}</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.1</version> </dependency>

参考列表

官网代码生成器部分

以上这篇浅谈mybatis 乐观锁实现,解决并发问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计1589个文字,预计阅读时间需要7分钟。

如何通过MyBatis实现乐观锁机制,有效应对并发冲突?

场景展示:银行两操作员同时操作同一账户即是典型例子。例如,A、B操作员同时读取金额为1000元的账户,A操作员为该账户增加100元,B操作员为该账户扣除50元,先提交A后提交B。

情景展示:

银行两操作员同时操作同一账户就是典型的例子。

比如A、B操作员同时读取一余额为1000元的账户,A操作员为该账户增加100元,B操作员同时为该账户扣除50元,A先提交,B后提交。最后实际账户余额为1000-50=950元,但本该为1000+100-50=1050。这就是典型的并发问题。

乐观锁机制在一定程度上解决了这个问题。乐观锁,大多是基于数据版本(Version)记录机制实现。何谓数据版本?即为数据增加一个版本标识,在基于数据库表的版本解决方案中,一般是通过为数据库表增加一个 “version” 字段来实现。

读取出数据时,将此版本号一同读出,之后更新时,对此版本号加一。此时,将提交数据的版本数据与数据库表对应记录的当前版本信息进行比对,如果提交的数据版本号大于数据库表当前版本号,则予以更新,否则认为是过期数据。

如何通过MyBatis实现乐观锁机制,有效应对并发冲突?

对于上面修改用户帐户信息的例子而言,假设数据库中帐户信息表中有一个version字段,当前值为1;而当前帐户余额字段(balance)为1000元。假设操作员A先更新完,操作员B后更新。

a、操作员A此时将其读出(version=1),并从其帐户余额中增加100(1000+100=1100)。

b、在操作员A操作的过程中,操作员B也读入此用户信息(version=1),并从其帐户余额中扣除50(1000-50=950)。

c、操作员A完成了修改工作,将数据版本号加一(version=2),连同帐户增加后余额(balance=1100),提交至数据库更新,此时由于提交数据版本大于数据库记录当前版本,数据被更新,数据库记录version更新为2。

d、操作员B完成了操作,也将版本号加一(version=2)试图向数据库提交数据(balance=950),但此时比对数据库记录版本时发现,操作员B提交的数据版本号为2,数据库记录当前版本也为2,不满足 “提交版本必须大于记录当前版本才能执行更新 “的乐观锁策略,因此,操作员B的提交被驳回。

这样,就避免了操作员B用基于version=1的旧数据修改的结果覆盖操作员A的操作结果的可能。

示例代码:

account建库脚本

drop table if exists account_wallet; /*==============================================================*/ /* Table: account_wallet */ /*==============================================================*/ create table account_wallet ( id int not null comment '用户钱包主键', user_open_id varchar(64) comment '用户中心的用户唯一编号', user_amount decimal(10,5), create_time datetime, update_time datetime, pay_password varchar(64), is_open int comment '0:代表未开启支付密码,1:代表开发支付密码', check_key varchar(64) comment '平台进行用户余额更改时,首先效验key值,否则无法进行用户余额更改操作', version int comment '基于mysql乐观锁,解决并发访问' primary key (id) );

dao层

AccountWallet selectByOpenId(String openId);

int updateAccountWallet(AccountWallet record);

service 层

AccountWallet selectByOpenId(String openId);

int updateAccountWallet(AccountWallet record);

serviceImpl层

public AccountWallet selectByOpenId(String openId) { // TODO Auto-generated method stub return accountWalletMapper.selectByOpenId(openId); } public int updateAccountWallet(AccountWallet record) { // TODO Auto-generated method stub return accountWalletMapper.updateAccountWallet(record); }

sql.xml

<!--通过用户唯一编号,查询用户钱包相关的信息 --> <select id="selectByOpenId" resultMap="BaseResultMap" parameterType="java.lang.String"> select <include refid="Base_Column_List" /> from account_wallet where user_open_id = #{openId,jdbcType=VARCHAR} </select> <!--用户钱包数据更改 ,通过乐观锁(version机制)实现 --> <update id="updateAccountWallet" parameterType="com.settlement.model.AccountWallet"> <![CDATA[ update account_wallet set user_amount = #{userAmount,jdbcType=DECIMAL}, version = version + 1 where id =#{id,jdbcType=INTEGER} and version = #{version,jdbcType=INTEGER} ]]> </update>

controller 层

package com.settlement.controller; import java.math.BigDecimal; import javax.servlet.localhost:8080/Settlement/wallet/walleroptimisticlock.action", "openId="+openId+"&openType="+openType+"&amount="+amount); System.out.println("操作结果:"+result); System.out.println("模拟用户: "+workerName+" 模拟请求结束 at "+sdf.format(new Date())); } } }

补充知识:Mybatis-plus代码生成器,自用版本不带xml

package com.wuyd.mybatispulsdemo; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; /** * @author wuyd * 创建时间:2019/10/8 11:17 */ public class CodeGenerator { public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); //全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(System.getProperty("user.dir")+"/src/main/java"); gc.setFileOverride(true); //不需要ActiveRecord特性的请改为false gc.setActiveRecord(true); gc.setSwagger2(true); gc.setAuthor("wuyd"); //自定义文件命名,注意%s 会自动填充表实体属性 gc.setControllerName("%sController"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setEntityName("%sEntity"); gc.setMapperName("%sMapper"); mpg.setGlobalConfig(gc); //数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("xxx"); dsc.setPassword("xxx"); dsc.setUrl("jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxxx?useUnicode=true&useSSL=false&characterEncoding=utf8"); mpg.setDataSource(dsc); //策略配置 StrategyConfig strategy = new StrategyConfig(); //此处可以修改您的表前缀 strategy.setTablePrefix(new String[]{}); //表名生成策略 strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); //需要生成的表 strategy.setInclude(new String[]{"knapsacks","knapsacks_kind","knapsacks_prop","knapsacks_recharge_card"}); strategy.setSuperServiceClass(null); strategy.setSuperServiceImplClass(null); strategy.setSuperMapperClass(null); strategy.setControllerMappingHyphenStyle(true); strategy.setEntityLombokModel(true); strategy.setEntitySerialVersionUID(true); strategy.setEntityTableFieldAnnotationEnable(true); mpg.setStrategy(strategy); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); //包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.wuyd.mybatispulsdemo"); pc.setController("controller"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setMapper("mapper"); pc.setEntity("entity"); mpg.setPackageInfo(pc); //执行生成 mpg.execute(); } }

pom.xml

<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.2.0</version> </dependency> <!-- ORM 选一款 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus-boot-starter.version}</version> </dependency> <!-- Mysql驱动 注意版本!--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector.version}</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.1</version> </dependency>

参考列表

官网代码生成器部分

以上这篇浅谈mybatis 乐观锁实现,解决并发问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。