当前位置:网站首页>Lock wait timeout exceeded try restarting transaction

Lock wait timeout exceeded try restarting transaction

2022-07-06 12:35:00 wx5caecf2ed0645


There have been frequent occurrences in recent projects   Lock wait timeout exceeded; try restarting transaction This mistake , It makes us miserable , To solve the problem , Find a lot of information on the Internet , Finally, the problem reappears . The specific operation steps are as follows ( I use mysql The tool is  navicat):

First step : Turn on sql Command page

 Deadlock problems encountered in the project Lock wait timeout exceeded try restarting transaction_sql

The second step : Input start transaction; Start a transaction

The third step : Input update sentence   UPDATE TABLENAME set time=null where id=29163;( Remember not to   Submit (commit;))


Step four : Writing tests


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36



​public​​​ ​​class​​​ ​​StTest ​​​​implements​​​ ​​Runnable{​

​private​​​  ​​static​​​ ​​ApplicationContext applicationContext=​​​​null​​​​;​

​private​​​ ​​static​​​ ​​AtomicInteger a=​​​​new​​​ ​​AtomicInteger(​​​​0​​​​);​

​static​​​ ​​{​

​applicationContext=​​​​new​​​ ​​ClassPathXmlApplicationContext(​​​​"classpath:application.xml"​​​​);​

​}​

​public​​​ ​​static​​​ ​​void​​​ ​​main(String[] args) ​​​​throws​​​ ​​Exception {​

​ExecutorService executorService = Executors.newFixedThreadPool(​​​​10​​​​);​

​for​​​ ​​(​​​​int​​​ ​​i = ​​​​0​​​​; i <​​​​10​​​ ​​; i++) {​

​executorService.execute(​​​​new​​​ ​​aaaa());​

​}​

​if​​​​(!executorService.isTerminated()){​

​executorService.shutdown();​

​}​

​}​


​@Override​

​public​​​ ​​void​​​ ​​run() {​

​XXXService bean = applicationContext.getBean(XXXService.​​​​class​​​​);​

​XXXBeand xx= ​​​​null​​​​;​

​try​​​ ​​{​

​xx= bean.byId(​​​​254213​​​​);​

​} ​​​​catch​​​ ​​(Exception e) {​

​e.printStackTrace();​

​}​

​long​​​ ​​aaa=System.currentTimeMillis();​

​for​​​ ​​(​​​​int​​​ ​​i = ​​​​0​​​​; i < ​​​​10​​​​; i++) {​

​try​​​ ​​{​

​bean.update(xx);​

​} ​​​​catch​​​ ​​(Exception e) {​

​e.printStackTrace();​

​}​

​}​

​System.out.println(System.currentTimeMillis()-aaa);​

​}​

​}​



Run the program and run the following 3 individual sql View results


1

2

3



​SELECT * FROM information_schema.INNODB_TRX;​

​select * from information_schema.innodb_lock_waits;​

​select * from information_schema.innodb_locks;​


   Get the following run results . We found several others  trx_state  Status is LOCK WAIT, Get the result we want to reproduce

  Deadlock problems encountered in the project Lock wait timeout exceeded try restarting transaction_mysql_02


Step five : Wait for the error to appear , After waiting for a period of time, we will find that the program reports an error


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32



​Caused by: java.sql.SQLException: Lock wait timeout exceeded; ​​​​try​​​ ​​restarting transaction​

​at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:​​​​1055​​​​)​

​at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:​​​​956​​​​)​

​at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:​​​​3491​​​​)​

​at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:​​​​3423​​​​)​

​at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:​​​​1936​​​​)​

​at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:​​​​2060​​​​)​

​at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:​​​​2542​​​​)​

​at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:​​​​1734​​​​)​

​at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:​​​​995​​​​)​

​at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:​​​​493​​​​)​

​at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)​

​at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:​​​​43​​​​)​

​at java.lang.reflect.Method.invoke(Method.java:​​​​606​​​​)​

​at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:​​​​55​​​​)​

​at com.sun.proxy.$Proxy17.execute(Unknown Source)​

​at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:​​​​41​​​​)​

​at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:​​​​66​​​​)​

​at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:​​​​45​​​​)​

​at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:​​​​100​​​​)​

​at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:​​​​75​​​​)​

​at sun.reflect.GeneratedMethodAccessor35.invoke(Unknown Source)​

​at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:​​​​43​​​​)​

​at java.lang.reflect.Method.invoke(Method.java:​​​​606​​​​)​

​at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:​​​​59​​​​)​

​at com.sun.proxy.$Proxy15.update(Unknown Source)​

​at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:​​​​148​​​​)​

​at sun.reflect.GeneratedMethodAccessor34.invoke(Unknown Source)​

​at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:​​​​43​​​​)​

​at java.lang.reflect.Method.invoke(Method.java:​​​​606​​​​)​

​at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:​​​​354​​​​)​

​... ​​​​20​​​ ​​more​



The results obtained from the above , You can know that this error occurs when a transaction is not committed , Other transactions also operate on the same object , So we found the cause of the problem , We can modify it according to this situation .


This is the mistake I encountered this time , Let me share with you , To avoid such a situation in the program .


原网站

版权声明
本文为[wx5caecf2ed0645]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131527440952.html