這篇文章主要介紹“如何解決Lock wait timeout exceeded的問題”,在日常操作中,相信很多人在如何解決Lock wait timeout exceeded的問題問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何解決Lock wait timeout exceeded的問題”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
最近在排查問題時發(fā)現(xiàn),偶爾會發(fā)生關(guān)于數(shù)據(jù)庫鎖超時的現(xiàn)象,會發(fā)生像如下的報錯信息:
Exception in thread "pool-3-thread-1" org.springframework.dao.CannotAcquireLockException: ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction ### The error may involve com.zr.center.mybatis.auto.mapper.UserMapper.updateByExampleSelective-Inline ### The error occurred while setting parameters ### SQL: update user SET user_name = ? WHERE ( user_id = ? ) ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction ; ]; Lock wait timeout exceeded; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:262) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447) at com.sun.proxy.$Proxy101.update(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:295) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:59) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) at com.sun.proxy.$Proxy103.updateByExampleSelective(Unknown Source) at com.zr.center.framework.web.service.BaseService.updateByExampleSelective(BaseService.java:97) at com.zr.center.api.test.service.TestService.updateUserName(TestService.java:34) at com.zr.center.api.test.service.TestService$$FastClassBySpringCGLIB$$bd3aa32.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) at com.zr.center.api.test.service.TestService$$EnhancerBySpringCGLIB$$59b19302.updateUserName(<generated>) at com.zr.center.ApplicationTests.lambda$testTxLockWaiting$0(ApplicationTests.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:952) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
經(jīng)過排查,DBA給出的日志中并未有死鎖,死鎖的原因排除,查詢業(yè)務(wù)日志發(fā)現(xiàn)在高并發(fā)的時期有時會有重復(fù)請求過來,也有一個服務(wù)在處理某個邏輯時會發(fā)一條mq消息,而同時會消費這條消息,此時也會導(dǎo)致鎖超時。超時原因就是因為一個事務(wù)中處理的邏輯過多,有調(diào)外部服務(wù)(超時),有更新其它多張表的操作,這樣就會導(dǎo)致后面事務(wù)請求超時,報以上錯誤。
關(guān)于配置信息查看,可以看到事務(wù)隔離是RR,事務(wù)鎖等待時長為默認(rèn)的50s
/** * @description: 用戶服務(wù)超時測試 * @author: chong guo * @create: 2018-12-10 14:44 */ @Service @Slf4j public class TestService extends BaseService<User, UserExample, Long> { /** * 更新用戶名稱 * * @param userId * @param name */ @Transactional(rollbackFor = Exception.class) public void updateUserName(Long userId, String name) throws InterruptedException { log.info("開始更新用戶名【{}】,用戶ID為【{}】", name, userId); UserExample userExample = new UserExample(); userExample.createCriteria().andUserIdEqualTo(userId); User user = new User(); user.setUserName(name); super.updateByExampleSelective(user, userExample); // 模擬業(yè)務(wù)超時,有可能調(diào)用外部遠(yuǎn)程服務(wù)超時,也有可能處理其它邏輯 Thread.sleep(55000); log.info("結(jié)束更新用戶名【{}】,用戶ID為【{}】", name, userId); } }
/** * @author Chong Guo */ @RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class ApplicationTests { @Resource TestService testService; private final int threadCount = 5; @Test public void testTxLockWaiting() throws InterruptedException { CountDownLatch countDownLatch = new CountDownLatch(threadCount); ExecutorService threadPool = Executors.newFixedThreadPool(300); for (int i = 0; i < threadCount; i++) { threadPool.execute(() -> { try { testService.updateUserName(611526166943105024L, "chongguo"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { countDownLatch.countDown(); } }); } countDownLatch.await(); threadPool.shutdown(); log.info("Test tx lock is over"); Thread.sleep(100000); } }
運行代碼后會現(xiàn)三次失敗,二次成功,失敗原因都是鎖超時
到此,關(guān)于“如何解決Lock wait timeout exceeded的問題”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。