How to commit transaction in Keycloak?

532 Views Asked by At

I work with Keycloak:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
    <artifactId>keycloak-parent</artifactId>
    <groupId>org.keycloak</groupId>
    <version>4.4.0.21-SNAPSHOT</version>
</parent>

<name>Keycloak WildFly Integration</name>
<description/>
<modelVersion>4.0.0</modelVersion>

<artifactId>keycloak-wildfly-parent</artifactId>
<packaging>pom</packaging>

<modules>
    <module>adduser</module>
    <module>extensions</module>
    <module>server-subsystem</module>
</modules>

And in my custom module I'm trying to work with transactions. I get this code from main class KeycloakApplications as an example

KeycloakSession session = sessionFactory.create();
   try {
       session.getTransactionManager().begin();
       //do some wokr with db etc.
       session.getTransactionManager().commit();
       ServicesLogger.LOGGER.addUserSuccess(userRep.getUsername(), realmRep.getRealm());
   }

But when I use the same method in my code I get this error

java.lang.IllegalStateException: No transaction associated with the current thread

Full stactrace:

15:23:36,668 ERROR [com.mycompany.sso.events.listener.KafkaConsumerProvider] (keycloak-audit-akka.actor.default-dispatcher-5) No transaction associated with the current thread: java.lang.IllegalStateException: No transaction associated with the current thread at org.wildfly.transaction.client.ContextTransactionManager.commit(ContextTransactionManager.java:69) at org.keycloak.transaction.JtaTransactionWrapper.commit(JtaTransactionWrapper.java:92) at org.keycloak.services.DefaultKeycloakTransactionManager.commit(DefaultKeycloakTransactionManager.java:136) at com.mycompany.sso.events.listener.KafkaConsumerProvider.process(KafkaConsumerProvider.java:79) at akka.stream.javadsl.Source.$anonfun$map$1(Source.scala:1299) at akka.stream.impl.fusing.Map$$anon$1.onPush(Ops.scala:54) at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:523) at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:480) at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:376) at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:606) at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:485) at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:581) at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:749) at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:764) at akka.actor.Actor.aroundReceive(Actor.scala:539) at akka.actor.Actor.aroundReceive$(Actor.scala:537) at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:671) at akka.actor.ActorCell.receiveMessage(ActorCell.scala:614) at akka.actor.ActorCell.invoke(ActorCell.scala:583) at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:268) at akka.dispatch.Mailbox.run(Mailbox.scala:229) at akka.dispatch.Mailbox.exec(Mailbox.scala:241) at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

My code:

try {
        KeycloakTransactionManager transactionManager = session.getTransactionManager();
        transactionManager.begin();
        //do some work
        transactionManager.commit(); //error in this line
    }

I debug class org.keycloak.services.DefaultKeycloakTransactionManager with implementations of begin(), commit() and rollback() methods, but it didn't help me figure out what I was doing wrong.

Some screens:

  1. TransactionManager before begin() enter image description here

  2. Transaction manager after begin() enter image description here

  3. Transaction manager after some work, but before commit enter image description here

Could anyone tell me why this error is happening?

1

There are 1 best solutions below

0
Pavel Bukhalov On

My mistake is that I do few transations in one session. It is problematic in Keycloak (see https://lists.jboss.org/pipermail/keycloak-user/2014-September/000898.html) When I create new session each time, it worked.

But maybe there is more correct way to solve my problem?