Deadlock Issues while Realm Deletion

Hi Experts,

I am facing an issue where some realms fail to delete while running tests which are combination of multiple realm creation and deletion.
We are using Keycloak-4.7.0-final running in clustered mode. If someone already have came across this issue can suggest any best practises to follow or bad practise to avoid to take care this issue.

2020-07-28 00:19:51,691 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-103) Deadlock found when trying to get lock; try restarting transaction
2020-07-28 00:19:51,692 ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-103) Uncaught server error: javax.persistence.OptimisticLockException: org.hibernate.exception.LockAcquisitionException: could not execute statement
at org.hibernate.internal.ExceptionConverterImpl.wrapLockException(ExceptionConverterImpl.java:277)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:98)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1604)
at org.keycloak.models.jpa.JpaUserProvider.preRemove(JpaUserProvider.java:376)
at org.keycloak.storage.UserStorageManager.preRemove(UserStorageManager.java:655)
at org.keycloak.models.cache.infinispan.UserCacheSession.preRemove(UserCacheSession.java:829)
at org.keycloak.models.jpa.JpaRealmProvider.removeRealm(JpaRealmProvider.java:140)
at org.keycloak.models.cache.infinispan.RealmCacheSession.removeRealm(RealmCacheSession.java:486)

Regards
Gaurav

Try upgrading first?
Have you checked all fixed issues since that version?
This could be a non issue to start with.

as i understand, this could be because when delete happens a many index in mysql needs to be adjusted, and possibly row locks may be causing the dead locks.

In general, does the transaction reaper in keycloak do a retry? is there any way to configure the wildfly to retry these?

A deadlock is a concurrency problem, 2 transactions try to access the same data at the same time. Both are waiting for each other.
This seems like a straightforward bug, that’s why I proposed to upgrade first or check the fixed issues since your version.
With Keycloak upgrades comes also Wildfly updates.