本文共 668 字,大约阅读时间需要 2 分钟。
可以参考这篇文章进行排查:
- 减小事务中的语句数量(代码的事务涉及行数过多,锁范围太大,很容易造成死锁)
- 在业务中调整语句的执行顺序,例如可以按照where条件中字段的大小进行一下排序,按照排序后顺序执行,让死锁变为锁等待。
也可以采用重试机制:
在Application.class加上
@EnableRetry
//LockAcquisitionException是RuntimeException @Retryable(maxAttempts = 4, value = RuntimeException.class, backoff = @Backoff(delay = 500)) public void doSomethingWithMysql() { consumerTransactionTemplate.execute( new TransactionCallbackWithoutResult(){ @Override protected void doInTransactionWithoutResult( TransactionStatus status) { process(); } }); }
Ref: db80
的回答
转载地址:http://ciztb.baihongyu.com/