Deadlock with concurrent requests to Microsoft SQL Server

Solution Verified - Updated -

Issue

When concurrent request queries are issued to Microsoft SQL Server, one request query succeeds, whereas the other fails with error message:

2020-11-24T14:55:09.095Z keycloak INFO  [com.XYZcompany.keycloak.register.UsersManagementResourceProvider] (default task-2) Creating user with XXX:RegisterUserRequest(username=rh1-1-0000, roles=null)
2020-11-24T14:55:13.363Z keycloak WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) SQL Error: 1205, SQLState: 40001
2020-11-24T14:55:13.364Z keycloak ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) Transaction (Process ID 54) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
2020-11-24T14:55:13.373Z keycloak ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-1) Uncaught server error: javax.persistence.OptimisticLockException: org.hibernate.exception.LockAcquisitionException: could not execute query
    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.list(AbstractProducedQuery.java:1514)
    at org.hibernate.query.Query.getResultList(Query.java:132)
    at org.keycloak.models.jpa.UserAdapter.getRoleMappings(UserAdapter.java:468)
    at org.keycloak.models.jpa.UserAdapter.hasRole(UserAdapter.java:420)
    at org.keycloak.models.jpa.UserAdapter.grantRole(UserAdapter.java:434)
    at org.keycloak.models.utils.DefaultRoles.addDefaultRoles(DefaultRoles.java:48)
    at org.keycloak.models.jpa.JpaUserProvider.addUser(JpaUserProvider.java:107)
    at org.keycloak.models.jpa.JpaUserProvider.addUser(JpaUserProvider.java:127)
    at org.keycloak.storage.UserStorageManager.addUser(UserStorageManager.java:151)
    at org.keycloak.models.cache.infinispan.UserCacheSession.addUser(UserCacheSession.java:768)
    at com.XYZcompany.keycloak.register.UsersManagementResourceProvider.createUser(UsersManagementResourceProvider.java:221)
    at com.XYZcompany.keycloak.register.UsersManagementResourceProvider.registerUser(UsersManagementResourceProvider.java:138)
    ...
    ...
Caused by: org.hibernate.exception.LockAcquisitionException: could not execute query
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    at org.hibernate.loader.Loader.doList(Loader.java:2692)
    ...
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Transaction (Process ID 54) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:259)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet$FetchBuffer.nextRow(SQLServerResultSet.java:6388)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.fetchBufferNext(SQLServerResultSet.java:1647)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(SQLServerResultSet.java:988)
    at org.jboss.jca.adapters.jdbc.WrappedResultSet.next(WrappedResultSet.java:2689)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:986)
    at org.hibernate.loader.Loader.doQuery(Loader.java:948)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
    at org.hibernate.loader.Loader.doList(Loader.java:2689)
    ... 

Environment

  • Red Hat Single Sign-On
    • 7
  • Microsoft SQL server
  • Concurrent requests to Microsoft SQL Server
  • Custom User Management Resource Provider

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content