-
MyBatis事务并发控制锁定问题-很心焦0
我们系统采用springMVC+MyBatis的架构,数据使用的是mysql,数据库的隔离级别是默认的:REPEATABLE-READ。现在发现一个事务并发控制锁定问题。
我们系统有一个业务逻辑,每个人只能执行一次,所以开启事务的时候,我们使用悲观锁进行控制:select * from table where type = 1 and id = XXX for update;如果用户点击非常快,点击两次的话,就会启动两个事务进行控制,暂定A和B两个事务。假如A事务先获得锁,那么只有A事务执行完成之后B事务才能获得锁,B事务就会处于等待状态。A事务在处理完成之后,就会把类型type修改了,改成了其他的值,比如说:2,但是B事务在获得了锁之后,即本语句select * from table where type = 1 and id = XXX for update;能够执行通过,但是该用户的type还是1,即还是A事务修改之前的数值。
请各位大牛多多帮忙!!!
小弟感激不尽!!!2014年11月03日 17:16
8个答案 按时间排序 按投票排序
-
修改你的SQL:去除查询条件type = 1 。事务A获取锁后,在代码中过虑出需要更新的记录,然后更新记录,提交事务。此时事务B就可以获取锁,查询出需要更新的最新记录(被事务A更新后的记录),再在代码中过虑需要更新的记录(逻辑同事务A),及可解决你的问题。
2014年11月07日 14:31
-
select * from table where type in ( select type from table where id =xxx ) and id = XXX for update;
2014年11月05日 15:33
-
考虑以下做法:
1)考虑将事务的隔离级别修改为read-commited;
2)事务B应该后续会做一定的更新的,可以在更新后面加一个type条件,比如说update table set xxxx where id=xxx and type=xxx,避免重复更新。根据返回结果的数量(如果是0的话表示已经被其他人更新过了)决定程序如何处理(放弃或者重试)2014年11月04日 08:01
-
幻影读,必须加range锁。
比如你应该用
select * from table where id = XXX for update;
去悲观锁一片数据
获得锁之后(另一个事务提交后)
select * from table where type = 1 and id = XXX for update;
就可以取得正确的数据了。2014年11月03日 17:48
相关推荐
赠送jar包:mybatis-plus-boot-starter-3.5.1.jar; 赠送原API文档:mybatis-plus-boot-starter-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-boot-starter-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-...
赠送jar包:mybatis-plus-extension-3.5.1.jar; 赠送原API文档:mybatis-plus-extension-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-extension-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2开发工具 mybatis-3.4.2...
赠送jar包:mybatis-generator-core-1.3.7.jar; 赠送原API文档:mybatis-generator-core-1.3.7-javadoc.jar; 赠送源代码:mybatis-generator-core-1.3.7-sources.jar; 赠送Maven依赖信息文件:mybatis-generator-...
本工程为 MyBatis-Plus 的官方示例,项目结构如下: mybatis-plus-sample-quickstart: 快速开始示例 mybatis-plus-sample-quickstart-springmvc: 快速开始示例(Spring MVC版本) mybatis-plus-sample-reduce-...
mybatis-3.5.9 源码(mybatis-3-mybatis-3.5.9.tar.gz)
赠送jar包:mybatis-spring-boot-autoconfigure-1.3.2.jar; 赠送原API文档:mybatis-spring-boot-autoconfigure-1.3.2-javadoc.jar; 赠送源代码:mybatis-spring-boot-autoconfigure-1.3.2-sources.jar; 赠送...
赠送jar包:mybatis-plus-boot-starter-3.1.0.jar; 赠送原API文档:mybatis-plus-boot-starter-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-boot-starter-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-...
赠送jar包:mybatis-plus-core-3.1.0.jar; 赠送原API文档:mybatis-plus-core-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-core-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-core-3.1.0.pom; ...
mybatis-plus就很好的解决了这个问题。 MyBatisPlus是一个Mybatis的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。它主要通过启动时自动注入基本CURD,达到简化操作的目的。 关于...
mybatis-3.2.7.jar mybatis-3.2.2.jar mybatis-3.1.1.jar
赠送jar包:mybatis-plus-annotation-3.1.0.jar; 赠送原API文档:mybatis-plus-annotation-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
mybatis mybatis-spring-boot-starter-2.0.0.jar下载
赠送jar包:mybatis-plus-annotation-3.5.1.jar; 赠送原API文档:mybatis-plus-annotation-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
赠送jar包:mybatis-spring-boot-autoconfigure-2.1.1.jar; 赠送原API文档:mybatis-spring-boot-autoconfigure-2.1.1-javadoc.jar; 赠送源代码:mybatis-spring-boot-autoconfigure-2.1.1-sources.jar; 赠送...
赠送jar包:mybatis-generator-core-1.3.5.jar; 赠送原API文档:mybatis-generator-core-1.3.5-javadoc.jar; 赠送源代码:mybatis-generator-core-1.3.5-sources.jar; 包含翻译后的API文档:mybatis-generator...
mybatis-spring-boot-starter-2.1.3.jarmybatis-spring-boot-starter-2.1.3.jarmybatis-spring-boot-starter-2.1.3.jar
赠送jar包:mybatis-spring-boot-autoconfigure-2.1.1.jar; 赠送原API文档:mybatis-spring-boot-autoconfigure-2.1.1-javadoc.jar; 赠送源代码:mybatis-spring-boot-autoconfigure-2.1.1-sources.jar; 赠送...
mybatis源码是mybatis-3.2.6的源码,用户使用mybatis时可以进行参考
mybatis-spring-boot-starter-2.1.4.jarmybatis-spring-boot-starter-2.1.4.jar