mysql 為何insert的時候會有l(wèi)ock wait timeout 異常
問題描述
執(zhí)行單元測試(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
但是像這樣的錯誤 不應(yīng)該只是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:我相信你已經(jīng)知道此種現(xiàn)象是發(fā)生鎖表了,lock wait timeout 意思就是由于鎖表導(dǎo)致事務(wù)等待的時間過長,然后事務(wù)回滾了我想你的疑問是(為什么insert 操作會鎖表)。
我的解釋如下:一般來說在普通狀態(tài)下,是不會發(fā)生此現(xiàn)象的,但是你主動啟動了事務(wù)(或者不主動提交事務(wù))就不同了,由于事務(wù)運行是需要一定時間并且將有多條指令運行,那么在此情況下,如果另外一個進程也開啟了事務(wù)并且鎖了表,并且無法短時間運行完的話(默認(rèn)記得是50秒),那么第一條事務(wù)會報這樣的錯并且回滾。所以他在錯誤中提示你try restarting transaction;讓你重新執(zhí)行
相關(guān)文章:
1. 請教使用PDO連接MSSQL數(shù)據(jù)庫插入是亂碼問題?2. node.js - nodejs開發(fā)中常用的連接mysql的庫3. python - 爬蟲模擬登錄后,爬取csdn后臺文章列表遇到的問題4. mysql - 分庫分表、分區(qū)、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處5. 視頻文件不能播放,怎么辦?6. 黑客 - Python模塊安全權(quán)限7. mysql - 把一個表中的數(shù)據(jù)count更新到另一個表里?8. Python爬蟲如何爬取span和span中間的內(nèi)容并分別存入字典里?9. python - 數(shù)據(jù)與循環(huán)次數(shù)對應(yīng)不上10. mysql 查詢身份證號字段值有效的數(shù)據(jù)
