CDI event bridge TransactionPhase.AFTER_COMPLETION problem

I have the following event handler:

@Singleton
public class GlobalProcessesListener {
//TODO: esto habra que mandarlo a un producer parametrizado según el bpkey o tener una clase listener x proceso
@Inject
private TaskService taskService;
@Inject
private ProcessEngine processEngine;
@Inject
private HistoryService historyService;

@Inject
private Logger logger;

protected static BusinessProcessEvent bpEvent;

 public void onProcessDuringTxEvents(@Observes(during = TransactionPhase.AFTER_COMPLETION) final BusinessProcessEvent businessProcessEvent) {

     String activityId = businessProcessEvent.getActivityId();
     boolean doIndex = false;

     if ((activityId != null && businessProcessEvent.getTask() == null &&
         businessProcessEvent.getType() == BusinessProcessEventType.START_ACTIVITY &&
         !activityId.contains("SubProcess") && !activityId.contains("Event") &&
         !activityId.contains("Gateway") && !(activityId.startsWith("mi")) &&
         !activityId.contains("Delete")) ||             (activityId == null && businessProcessEvent.getTask() != null &&
             (businessProcessEvent.getType() == BusinessProcessEventType.CREATE_TASK || businessProcessEvent.getType() == BusinessProcessEventType.COMPLETE_TASK))) {

         doIndex = true;
   }
   if (doIndex) {
       indexProcessInstance(businessProcessEvent, true);
   }
}

If I use AFTER_SUCCESS I get the following error:

11:30:40,184 ERROR [org.camunda.bpm.engine.context] (Camel (camel-2) thread #52 - JmsConsumer[EP_QUEUE_NEW_MASSIVE_PAYROLL]) ENGINE-16004 Exception while closing command context:

Error querying database. Cause: java.sql.SQLException: javax.resource.ResourceException: IJ000460: Error checking for a transaction

The error may exist in org/camunda/bpm/engine/impl/mapping/entity/HistoricProcessInstance.xml

The error may involve org.camunda.bpm.engine.impl.persistence.entity.HistoricProcessInstanceEntity.selectHistoricProcessInstancesByQueryCriteria

The error occurred while executing a query

Cause: java.sql.SQLException: javax.resource.ResourceException: IJ000460: Error checking for a transaction: org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: java.sql.SQLException: javax.resource.ResourceException: IJ000460: Error checking for a transaction

The error may exist in org/camunda/bpm/engine/impl/mapping/entity/HistoricProcessInstance.xml

The error may involve org.camunda.bpm.engine.impl.persistence.entity.HistoricProcessInstanceEntity.selectHistoricProcessInstancesByQueryCriteria

The error occurred while executing a query

If I use AFTER_COMPLETION when this code calls indexProcessInstance I get the following error, it looks as if it cannot open the jndi associated to my entity manager:

11:43:10,007 ERROR [org.jboss.as.ejb3] (Camel (camel-2) thread #52 - JmsConsumer[EP_QUEUE_NEW_MASSIVE_PAYROLL]) javax.ejb.EJBTransactionRolledbackException: JBAS014313: Could not lookup jndi name: org.jboss.as.ee.naming.InjectedEENamespaceContextSelector@4367a4d9 in context: org.jboss.as.naming.NamingContext@6e455c5
11:43:10,007 ERROR [org.jboss.as.ejb3.invocation] (Camel (camel-2) thread #52 - JmsConsumer[EP_QUEUE_NEW_MASSIVE_PAYROLL]) JBAS014134: EJB Invocation failed on component MultiTenantEntityManagerWrapper for method public abstract java.lang.Object javax.persistence.EntityManager.find(java.lang.Class,java.lang.Object): javax.ejb.EJBTransactionRolledbackException: JBAS014313: Could not lookup jndi name: org.jboss.as.ee.naming.InjectedEENamespaceContextSelector@4367a4d9 in context: org.jboss.as.naming.NamingContext@6e455c5
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleInCallerTx(CMTTxInterceptor.java:162) [jboss-as-ejb3-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19

This error also happens when I use BEFORE_COMPLETION, but it does not happen when I use IN_PROGRESS.

The problem with IN_PROGRESS is that when I call createTaskQuery().processInstanceId(processInstanceId) it returns null, even when I’m listening to a “create” event in a userTask. That’s why I’d like to listen to such a TransactionPhase that could return that current userTask from that createTaskQuery, instead of returning null.

I started investigating on Camunda CDI event bridge because I had the following error when sending a sendMessage:

09:52:05,446 ERROR [org.jboss.as.ejb3.invocation] (Camel (camel-2) thread #55 - JmsConsumer[EP_QUEUE_CALCULATEPAYROLLS]) JBAS014134: EJB Invocation failed on component PayrollNotificationProcessManagerImpl for method public abstract void es.gc.epsilon.core.api.processes.manager.ProcessActionManager.sendMessage(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.util.Map) throws es.gc.epsilon.core.commons.util.exceptions.InvalidProcessMessageException: javax.ejb.EJBTransactionRolledbackException: ENGINE-03005 Execution of ‘UPDATE VariableInstanceEntity[8d1a98df-f4b1-11e9-b3e6-8416f905c9a2]’ failed. Entity was updated by another transaction concurrently.

I also thought that it would be much cleaner to use global handlers whenever a tx is just commited instead of using listeners every now and then.

My goal is to index into elasticsearch (lucene based) a process intance dto on every tx commit.
Could you give me some hint about what am I doing wrong? What TransactionPhase should I use? I’ve read your documentation several times but I don’t find the solution…

Thanks in advance.