I try to use Hazelcast distributed data structures like IMap in a transactional context, but after every IMap Operation I get the same Exception.
java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManager.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.verifyOpen(EntityManagerImpl.java:2041)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.close(EntityManagerImpl.java:1886)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper$NonTxEMCleaner.beforePostInvoke(EntityManagerWrapper.java:1241)
at org.glassfish.api.invocation.InvocationManagerImpl$ListComponentInvocationHandler.lambda$beforePostInvoke$2(InvocationManagerImpl.java:430)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.glassfish.api.invocation.InvocationManagerImpl$ListComponentInvocationHandler.beforePostInvoke(InvocationManagerImpl.java:430)
at org.glassfish.api.invocation.InvocationManagerImpl.postInvoke(InvocationManagerImpl.java:198)
at fish.payara.appserver.context.ContextImpl$Context.close(ContextImpl.java:61)
at fish.payara.appserver.context.ContextImpl$RequestContext.close(ContextImpl.java:126)
at com.hazelcast.spi.impl.operationservice.Operation.popThreadContext(Operation.java:865)
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:281)
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:219)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.process(OperationThread.java:180)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.process(OperationThread.java:144)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.loop(OperationThread.java:134)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.executeRun(OperationThread.java:115)
at com.hazelcast.internal.util.executor.HazelcastManagedThread.run(HazelcastManagedThread.java:111)
Access would be like:
public IMap<String, UUID> getSOSChangeListenerMap() {
return hzInstance.getMap("statefulRoomSOSChangeListenerMap");
}
final IMap<String, UUID> sosChangeListenerMap = getSOSChangeListenerMap();
if (!sosChangeListenerMap.containsKey(apiNurseCallComponent.getIdentifier())) {
final UUID uuid = abstractStatefulDevice.getSOSChangeEventTopic().addMessageListener(getSosChangeListener());
sosChangeListenerMap.put(apiNurseCallComponent.getIdentifier(), uuid);
}
I already asked the question on stackoverflow.com but with no answers.
java - Hazelcast and Payara 5 - Tries to close entityManager if hazelcast is used in a transactional context - Stack Overflow
My Workaround is to execute every interaction with hazelcast maps in an seperate thread without any jakarta EE contexts. But especially when using ITopic and the appropriate MessageListeners I get the problem that obviously everything is outside of an Managed Jakarta EE context.
My workaround there is to execute everything in the message listener in an ManagedExecutorService but this only works partly, leading to:
[2023-08-21T16:05:35.862+0200] [Payara 5.2022.2] [WARNUNG] [] [javax.enterprise.resource.jta] [tid: ThreadID=93 ThreadName=hz.quizzical_grothendieck.event-5] [timeMillis: 1692626735862] [levelValue: 900] [[
TransactionalInterceptorBase.markThreadAsTransactional currentInvocation==null]]
[2023-08-21T16:05:35.862+0200] [Payara 5.2022.2] [WARNUNG] [AS-JTA-00003] [javax.enterprise.resource.jta] [tid: ThreadID=93 ThreadName=hz.quizzical_grothendieck.event-5] [timeMillis: 1692626735862] [levelValue: 900] [[
No ComponentInvocation present for @Transactional annotation processing. Restriction on use of UserTransaction will not be enforced.]]
How am I supposed to interact with Hazelcast so that the InvocationManager doesn’t try to close an entityManager which wasn’t used by the map operation in the first place.
BTW we do not interact with an EntityManager or EntityManagerFactory directly. We use only container managed EntityManagers provdided by
@PersistenceContext(
type = PersistenceContextType.TRANSACTION
)
private EntityManager em;
Any help would be greatly appreciated.