Vipul Pachauri
Vipul Pachauri

Reputation: 78

Getting LockAcquisitionException Exception in delete call, by using below code

While I call the method below, hibernate throws LockAcquisitionException.

Method to delete records :

@Transactional
@Modifying(clearAutomatically = true)
@Query(value = "DELETE FROM business_user where user_id in :userIds and business_id = :businessId",nativeQuery = true)
void deleteAllByUserIdsAndBusinessId(@Param("userIds") List<Integer> userIds,@Param("businessId") Integer businessId);

StackTrace :

021-04-26 20:18:48.149 ERROR [http-nio-8080-exec-199] c.b.b.w.e.h.APIExceptionHandler :Exception occured : 
javax.persistence.PersistenceException: org.hibernate.exception.LockAcquisitionException: could not execute statement
    ...
Caused by: org.hibernate.exception.LockAcquisitionException: could not execute statement
    ...
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction

Upvotes: 0

Views: 1187

Answers (1)

Grigorii Riabov
Grigorii Riabov

Reputation: 215

Your table locked by another transaction. (Maby this happens while you have a deadlock in some thread that cannot release database lock)

PS: Default isolation level of @Transaction depends on the datastore you use (link), check what level it is and if you need to try to down transaction isolation level. And try to research deadlocks in your program.

Upvotes: 1

Related Questions