Skip to content
Advertisement

UserTransaction jndi lookup failed when using CompletableFuture

I have a code which does context lookup to get UserTransaction JNDI as ctx.lookup("java:comp/UserTransaction").

When I run this code without using CompletableFuture, it works as expected.

When working with CompletableFuture in async thread, it gives exception saying jndi lookup failed.

I tried to check if I can get the required JNDI from global scope, but no luck.

Advertisement

Answer

The problem seems to be that the JNDI context is not propagated to the async thread, so when the CompletionStage attempts to execute the JNDI lookup, it has no context, so it doesn’t know which component it is in and thus fails.

There is a very detailed explanation of context propagation and how to do it effectively in Open Liberty (which is the underlying product for WebSphere Liberty) at https://openliberty.io/docs/21.0.0.8/microprofile-context-propagation.html – I’d highly suggest reading it.

Certain Java/Jakarta/MicroProfile APIs will allow you to specify the async service (ExecutorService) to use for the async operation. If possible, you can pass it an instance of ManagedExecutorService which should propagate contexts (like JNDI, security, classloading, etc.) to the async thread. Otherwise, you may need to specify the managed executor service when constructing your CompletionStage.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement