title: Proper way to use the hibernate api inside keycloak event listener
body: <p>I have a running Keycloak instance backed by a Postgres database. My intention is to use exposed resource management endpoints from the available <code>KeycloakSession</code> object inside my event listener provider to query and manipulate some table records after certain events.</p>
  <pre class="lang-scala prettyprint-override"><code>import org.keycloak.models.KeycloakSession
  import org.keycloak.events.EventListenerProvider
  import org.keycloak.events.Event
  import org.keycloak.events.admin.AdminEvent
  import org.keycloak.models.AbstractKeycloakTransaction
  import org.keycloak.events.admin.OperationType
  import scala.jdk.CollectionConverters.*
  
  class MyEventListenerProvider(session: KeycloakSession)
      extends EventListenerProvider {
  
    object paths:
      val users = &quot;users/(.+)&quot;.r // simplified for brevity
      val groups = &quot;groups/(.+)&quot;.r // simplified for brevity
  
    override def onEvent(event: Event) = ()
  
    override def onEvent(
        adminEvent: AdminEvent,
        includeRepresentation: Boolean
    ) =
      session
        .getTransactionManager()
        .enlist(new AbstractKeycloakTransaction {
          protected def commitImpl(): Unit = {
            (adminEvent.getOperationType(), adminEvent.getResourcePath()) match
              case (OperationType.CREATE, paths.groups(id)) =&gt;
                val realm = session.realms().getRealm(adminEvent.getRealmId())
                val group = session.groups().getGroupById(id, realm)
                // The line below fails WITH a wrapping transaction consistently
                // But works when it's not wrapped in a transaction
                // Error message: no transaction is in progress!
                List(&quot;provider&quot;, &quot;admin&quot;).foreach { subgroup =&gt;
                  session.groups().createGroup(realm, subgroup, group)
                }
              case (OperationType.UPDATE, paths.users(id)) =&gt;
                val realm = session.realms().getRealm(adminEvent.getRealmId())
                val user = session.users().getUserById(realm, id)
                // The line below fails WITH a wrapping transaction for a
                // couple of times before it succeeds after keycloak startup.
                // It also fails consistently WITHOUT a wrapping transaction
                // Error message: ResultSet is closed!
                user.getGroups().asScala.toSeq.map(_.getName())
                // Rest of the code does not matter; it fails at the line above
              case _ =&gt; ()
          }
  
          protected def rollbackImpl(): Unit = ()
        })
  
    override def close() = ()
  }
  </code></pre>
  <p>The errors I receive vary depending on which variation of the code above I run as explained by the inline code comments.</p>
  <p>My running Keycloak is version <code>18.0.0</code> and here is the list of my dependencies:</p>
  
  <pre><code>val KeycloakVersion = &quot;18.0.0&quot;
  
  libraryDependencies ++= Seq(
    &quot;org.keycloak&quot; % &quot;keycloak-core&quot; % KeycloakVersion % Provided,
    &quot;org.keycloak&quot; % &quot;keycloak-server-spi&quot; % KeycloakVersion % Provided,
    &quot;org.keycloak&quot; % &quot;keycloak-services&quot; % KeycloakVersion % Provided,
    &quot;org.keycloak&quot; % &quot;keycloak-saml-core-public&quot; % KeycloakVersion % Provided,
    &quot;org.keycloak&quot; % &quot;keycloak-server-spi-private&quot; % KeycloakVersion % Provided
  )
  </code></pre>