mysql 为何insert的时候会有lock wait timeout 异常
问题描述
执行单元测试(mvn clean test)时偶尔会遇到下面的异常信息
org.springframework.dao.CannotAcquireLockException:### Error updating database. Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction### The error may involve com.foo.dao.mapper.TestMapper.insertSuccesfulPaymentOrder-Inline### The error occurred while setting parameters### SQL: insert into order(order_seq,note,user_id,product_id, pay_status) values(uuid(),’’,?,?,1)### Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction; SQL []; Lock wait timeout exceeded; try restarting transaction; nested exception is java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
但是像这样的错误 不应该只是update才会有吗? 见官方文档说明
A transaction that tries to access a row that is locked by another InnoDB transaction waits at most this many seconds for write access to the row before issuing the following error:
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
但我明明是insert啊。 并且还只是偶尔会有这种情况, 且稍后就正常了。
所以不明白到底是怎么回事? 怎么模拟这种情况呢?
问题解答
回答1:我相信你已经知道此种现象是发生锁表了,lock wait timeout 意思就是由于锁表导致事务等待的时间过长,然后事务回滚了我想你的疑问是(为什么insert 操作会锁表)。
我的解释如下:一般来说在普通状态下,是不会发生此现象的,但是你主动启动了事务(或者不主动提交事务)就不同了,由于事务运行是需要一定时间并且将有多条指令运行,那么在此情况下,如果另外一个进程也开启了事务并且锁了表,并且无法短时间运行完的话(默认记得是50秒),那么第一条事务会报这样的错并且回滚。所以他在错误中提示你try restarting transaction;让你重新执行
相关文章:
1. dockerfile - 我用docker build的时候出现下边问题 麻烦帮我看一下2. java - 做数据库向手机端的推送要系统地学习哪些知识?求大神指点,谢谢!3. java - C语言的一道算法题-兄弟郊游问题 求解?4. java - Android Studio编译打包Apk出现错误5. html - 开发android的调取我web页面 他之前用的是收费的浏览器包 显示是全的换了免费的浏览器包后 显示就不全 是什么原因啊6. java - jdbc无法连接postgresql数据库7. java - SpringMvc没有根据&符号截取参数8. mongodb的java开发中使用两个字段相等查询条件应该怎么写?9. java编程思想第4版中文版的几处谬误?10. 需要一个Java映射/表,其中多个键指向一个值。价值通常会改变
![dockerfile - 我用docker build的时候出现下边问题 麻烦帮我看一下](http://www.haobala.com/attached/image/news/202311/1028095cc6.png)