Getting “Deadlock found when trying to get lock; try restarting transaction”

后端 未结 5 1756
一个人的身影
一个人的身影 2020-12-07 21:11

My Application(java spring-core) has several threads running concurrently and accessing db, I am getting exception in some peaktime

07:43:33,400 WARN  [org.         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 21:18

    If you are using JPA/Hibernate then simple just follow below steps to avoid dead lock. Once you have acquired the lock, don't made any call on db with same id anywhere in the transaction (I mean to say you should not get entity again on sameid), on locking object you modify and save no issues.

    service level:-

    employee=empDao.getForUpdate(id);
    

    Dao level:-

    public employee getForUpdate(String id)
    return mySqlRepository.getForUpdate(id)
    

    Repository(interface):-

    @Lock(LockModeType.PESSIMITSIC_WRITE)
    @Query("select e from employee e where id=?1")
    public employee getForUpdate(String id)
    

提交回复
热议问题