cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot flush EntityManager, an active transaction is required

omd
Champ in-the-making
Champ in-the-making
Hello,

I got this error, when I try to read bean variable ${person.firstName} in my next UserTask. But also my bean is not persisted in my db after call  service #{personService.persistPerson()} in serviceTask.  I think the problem is my entityExplorer doesn't share the same trasaction with Activiti. I can not figure out how to properly configure my activiti.cfg.xml. Here is my configuration, could you please check it out?:

my activiti.cfg.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   <!– lookup the JTA-Transaction manager –>
   <bean id="transactionManager" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="java:jboss/TransactionManager"></property>
      <property name="resourceRef" value="true" />
   </bean>

   <!– process engine configuration –>
   <bean id="processEngineConfiguration"
      class="org.activiti.cdi.CdiJtaProcessEngineConfiguration">
      <!– lookup the default Jboss datasource –>
      <property name="dataSourceJndiName" value="java:jboss/datasources/mydb" />
      <property name="databaseType" value="oracle" />
      <property name="transactionManager" ref="transactionManager" />
      <property name="jpaPersistenceUnitName" value="myapp" />
       <property name="jpaHandleTransaction" value="false" />
       <property name="jpaCloseEntityManager" value="true" />        
        <property name="jobExecutorActivate" value="false" />        
        <property name="transactionsExternallyManaged" value="true" />
      <property name="databaseSchemaUpdate" value="true" />
   </bean>

</beans>


my persistence.xml:
<blockcode>
<?xml version="1.0" encoding="utf-8"?>
<persistence version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
   
<persistence-unit name="myapp" transaction-type="RESOURCE_LOCAL">
      <jta-data-source>java:jboss/datasources/mydb</jta-data-source>
      
      <class>com.app.entity.Person</class>

      <properties>
      <property name="javax.persistence.schema-generation.database.action"
              value="drop-and-create" />
        
             <property name="hibernate.show_sql" value="true" />
      </properties>
   </persistence-unit>
</persistence>
</blockcode>

and my process:


<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="GrantingPrivilege" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start" activiti:initiator="initiator"></startEvent>
    <userTask id="usertask1" name="User Task" activiti:assignee="${initiator}">
      <documentation>Name is </documentation>
    </userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <scriptTask id="scripttask1" name="Script Task" scriptFormat="groovy" activiti:autoStoreVariables="false">
      <script>List&lt;String&gt; assigneeList = new ArrayList&lt;String&gt;();
assigneeList.add("kermit");
assigneeList.add("gonzo");
assigneeList.add("fozzie");
execution.setVariable("assigneeList", assigneeList );</script>
    </scriptTask>
    <userTask id="usertask3" name="User Task" activiti:assignee="${initiator}">
      <documentation></documentation>
    </userTask>
    <sequenceFlow id="flow7" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
    <subProcess id="subprocess1" name="Sub Process">
      <multiInstanceLoopCharacteristics isSequential="false" activiti:collection="assigneeList" activiti:elementVariable="assignee">
        <completionCondition>${decision == 'false'}</completionCondition>
      </multiInstanceLoopCharacteristics>
      <startEvent id="startevent2" name="Start"></startEvent>
      <userTask id="usertask4" name="User Task" activiti:assignee="${assignee}">
        <extensionElements>
          <activiti:formProperty id="decision" name="Decyzja" type="enum">
            <activiti:value id="false" name="Brak Zgody"></activiti:value>
            <activiti:value id="true" name="Zgoda"></activiti:value>
          </activiti:formProperty>
        </extensionElements>
      </userTask>
      <sequenceFlow id="flow8" sourceRef="startevent2" targetRef="usertask4"></sequenceFlow>
      <endEvent id="endevent2" name="End"></endEvent>
      <sequenceFlow id="flow9" sourceRef="usertask4" targetRef="endevent2"></sequenceFlow>
    </subProcess>
    <sequenceFlow id="flow10" sourceRef="scripttask1" targetRef="subprocess1"></sequenceFlow>
    <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
    <sequenceFlow id="flow11" sourceRef="subprocess1" targetRef="exclusivegateway1"></sequenceFlow>
    <userTask id="usertask5" name="User Task" activiti:assignee="${initiator}">
      <documentation></documentation>
    </userTask>
    <sequenceFlow id="flow12" sourceRef="exclusivegateway1" targetRef="usertask5">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${decision == 'false'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow13" sourceRef="exclusivegateway1" targetRef="usertask3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${decision == 'true'}]]></conditionExpression>
    </sequenceFlow>
    <endEvent id="endevent3" name="End"></endEvent>
    <sequenceFlow id="flow14" sourceRef="usertask5" targetRef="endevent3"></sequenceFlow>
    <serviceTask id="servicetask1" name="Service Task" activiti:expression="#{personService.persistPerson()}" activiti:resultVariableName="person"></serviceTask>
    <sequenceFlow id="flow15" sourceRef="usertask1" targetRef="servicetask1"></sequenceFlow>
    <userTask id="usertask6" name="User Task" activiti:assignee="${initiator}">
      <documentation>name is ${person.firstName}</documentation>
    </userTask>
    <sequenceFlow id="flow17" sourceRef="servicetask1" targetRef="usertask6"></sequenceFlow>
    <sequenceFlow id="flow18" sourceRef="usertask6" targetRef="scripttask1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_GrantingPrivilege">
    <bpmndi:BPMNPlane bpmnElement="GrantingPrivilege" id="BPMNPlane_GrantingPrivilege">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="60.0" y="230.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="130.0" y="220.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="1220.0" y="230.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="scripttask1" id="BPMNShape_scripttask1">
        <omgdc:Bounds height="55.0" width="105.0" x="350.0" y="220.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
        <omgdc:Bounds height="55.0" width="105.0" x="1070.0" y="220.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="subprocess1" id="BPMNShape_subprocess1">
        <omgdc:Bounds height="171.0" width="361.0" x="510.0" y="162.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="startevent2" id="BPMNShape_startevent2">
        <omgdc:Bounds height="35.0" width="35.0" x="550.0" y="232.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4">
        <omgdc:Bounds height="55.0" width="105.0" x="630.0" y="222.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">
        <omgdc:Bounds height="35.0" width="35.0" x="800.0" y="232.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="920.0" y="227.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask5" id="BPMNShape_usertask5">
        <omgdc:Bounds height="55.0" width="105.0" x="1070.0" y="100.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent3" id="BPMNShape_endevent3">
        <omgdc:Bounds height="35.0" width="35.0" x="1220.0" y="110.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
        <omgdc:Bounds height="55.0" width="105.0" x="180.0" y="319.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask6" id="BPMNShape_usertask6">
        <omgdc:Bounds height="55.0" width="105.0" x="360.0" y="330.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="95.0" y="247.0"></omgdi:waypoint>
        <omgdi:waypoint x="130.0" y="247.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
        <omgdi:waypoint x="1175.0" y="247.0"></omgdi:waypoint>
        <omgdi:waypoint x="1220.0" y="247.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
        <omgdi:waypoint x="585.0" y="249.0"></omgdi:waypoint>
        <omgdi:waypoint x="630.0" y="249.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
        <omgdi:waypoint x="735.0" y="249.0"></omgdi:waypoint>
        <omgdi:waypoint x="800.0" y="249.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
        <omgdi:waypoint x="455.0" y="247.0"></omgdi:waypoint>
        <omgdi:waypoint x="510.0" y="247.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11">
        <omgdi:waypoint x="871.0" y="247.0"></omgdi:waypoint>
        <omgdi:waypoint x="920.0" y="247.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow12" id="BPMNEdge_flow12">
        <omgdi:waypoint x="940.0" y="227.0"></omgdi:waypoint>
        <omgdi:waypoint x="940.0" y="127.0"></omgdi:waypoint>
        <omgdi:waypoint x="1070.0" y="127.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow13" id="BPMNEdge_flow13">
        <omgdi:waypoint x="960.0" y="247.0"></omgdi:waypoint>
        <omgdi:waypoint x="1070.0" y="247.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow14" id="BPMNEdge_flow14">
        <omgdi:waypoint x="1175.0" y="127.0"></omgdi:waypoint>
        <omgdi:waypoint x="1220.0" y="127.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow15" id="BPMNEdge_flow15">
        <omgdi:waypoint x="182.0" y="275.0"></omgdi:waypoint>
        <omgdi:waypoint x="232.0" y="319.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow17" id="BPMNEdge_flow17">
        <omgdi:waypoint x="285.0" y="346.0"></omgdi:waypoint>
        <omgdi:waypoint x="360.0" y="357.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow18" id="BPMNEdge_flow18">
        <omgdi:waypoint x="412.0" y="330.0"></omgdi:waypoint>
        <omgdi:waypoint x="402.0" y="275.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>




and my service:


@Named
public class PersonService {

   @PersistenceContext
   private EntityManager entityManager;

   public Person persistPerson(){
   
      Person person = new Person();
      person.setFirstName("John");   
      person.setLastName("Travolta");
           entityManager.persist(person);
      System.out.println(person.getFirstName());
      return person;
   }
}
1 REPLY 1

vasile_dirla
Star Contributor
Star Contributor
   didn't test this but I maybe this will work for you:

<code>
    @PersistenceUnit(unitName="myapp")
    private EntityManagerFactory entityManagerFactory;
</code>