How to Avoid Lock wait timeout exceeded exception.?

前端 未结 7 1221
独厮守ぢ
独厮守ぢ 2020-12-09 10:32
    java.sql.SQLException: Lock wait timeout exceeded; try restarting tra
nsaction at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
        at com.m         


        
相关标签:
7条回答
  • 2020-12-09 10:45

    (Respectfully ignoring DB specific answers)

    When using a formal CRUD schema with all DB traffic channelled through a Singleton class you can still encounter thread-locking. This often occurs due to an oversight between yourself, a college, and the Hibernate team. Therefore, it is quickest to review your own code for bugs -- paying particular attention to hibernate's core rules.

    Normally a CRUD interface has public 'Create', 'Read', 'Update' and 'Delete' methods that share common private methods. This is done for DRY best practise. However, in doing so, these methods will work flawlessly most of the time, but not all of the time.

    So, how to test and solve thread-locking?

    Ensure:

    • session.save(myObj) actions firstly check on the PK's uniqueness
    • All uniqueResult() queries indeed return 1 result, And;

      (Important!) Focus Test-cases that:

      • Introduce PK duplicates
      • Update/Read/Delete non-existent records/entries/rows

    Finally, use @AfterSuite (TestNG) to delete all table entries. Any insufficient implementation will yield another thread lock on the aforementioned operation ... otherwise, you are golden.

    0 讨论(0)
  • 2020-12-09 10:51

    Check if your where clause is optimized.. i.e. using primary key and/or indexes

    0 讨论(0)
  • 2020-12-09 10:56

    remove @Transactional(propagation = Propagation.REQUIRES_NEW) and check.It will work

    0 讨论(0)
  • 2020-12-09 11:00

    If all of above does not work, check the error message from mysql log. If it mentions about the size of log file, you need to increase it in the configuration and restart the mysql server.

    0 讨论(0)
  • 2020-12-09 11:01

    Here are some suggestions:

    1. Lock wait timeout’ occurs typically when a transaction is waiting on row(s) of data to update which is already been locked by some other transaction.
    2. Most of the times, the problem lies on the database side. The possible causes may be a inappropriate table design, large amount of data, constraints etc.
    3. Please check out this elaborate answer .
    0 讨论(0)
  • 2020-12-09 11:03

    Make sure the database tables are using InnoDB storage engine and READ-COMMITTED transaction isolation level.

    You can check it by SELECT @@GLOBAL.tx_isolation, @@tx_isolation; on mysql console.

    If it is not set to be READ-COMMITTED then you must set it. Make sure before setting it that you have SUPER privileges in mysql.

    You can take help from http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html.

    By setting this I think your problem will get solved.

    Thank You.

    0 讨论(0)
提交回复
热议问题