重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍“如何解决Lock wait timeout exceeded的问题”,在日常操作中,相信很多人在如何解决Lock wait timeout exceeded的问题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何解决Lock wait timeout exceeded的问题”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
10余年的张北网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整张北建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“张北网站设计”,“张北网站推广”以来,每个客户项目都认真落实执行。
最近在排查问题时发现,偶尔会发生关于数据库锁超时的现象,会发生像如下的报错信息:
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() 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( ) 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)
经过排查,DBA给出的日志中并未有死锁,死锁的原因排除,查询业务日志发现在高并发的时期有时会有重复请求过来,也有一个服务在处理某个逻辑时会发一条mq消息,而同时会消费这条消息,此时也会导致锁超时。超时原因就是因为一个事务中处理的逻辑过多,有调外部服务(超时),有更新其它多张表的操作,这样就会导致后面事务请求超时,报以上错误。
关于配置信息查看,可以看到事务隔离是RR,事务锁等待时长为默认的50s
/** * @description: 用户服务超时测试 * @author: chong guo * @create: 2018-12-10 14:44 */ @Service @Slf4j public class TestService extends BaseService{ /** * 更新用户名称 * * @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); // 模拟业务超时,有可能调用外部远程服务超时,也有可能处理其它逻辑 Thread.sleep(55000); log.info("结束更新用户名【{}】,用户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); } }
运行代码后会现三次失败,二次成功,失败原因都是锁超时
到此,关于“如何解决Lock wait timeout exceeded的问题”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!