mysql 为何insert的时候会有lock wait timeout 异常
PHPz
PHPz 2017-04-17 14:56:40
0
1
789

执行单元测试(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啊。 并且还只是偶尔会有这种情况, 且稍后就正常了。

所以不明白到底是怎么回事? 怎么模拟这种情况呢?

PHPz
PHPz

学习是最好的投资!

全員に返信(1)
小葫芦

この現象は、ロック テーブルが発生したときに発生することはすでにご存知だと思いますが、ロック テーブルが原因でトランザクションが長時間待機し、その後トランザクションがロールバックされることを意味します。
ご質問は (なぜ) だと思います。 insert 操作によりテーブルがロックされます)。

私の説明は次のとおりです:
一般に、この現象は通常の状況では発生しませんが、トランザクションをアクティブに開始する (またはトランザクションをアクティブに送信しない) 場合は異なります。トランザクション操作には、この場合、別のプロセスもトランザクションを開始してテーブルをロックし、短時間 (デフォルトは 50 秒) で完了できない場合、最初のプロセスが実行されます。トランザクションはそのようなエラーを報告し、ロールバックします。
そのため、エラーでトランザクションを再実行するように求められます。

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート