cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot flush EntityManager, an active transaction is require

javatech
Champ in-the-making
Champ in-the-making
Hi all,
activiti works fine if return type is void, if method returns "JPA Entity" 'LoanRequest'
is throws Cannot flush EntityManager, an active transaction is require.
@Transactional()
//public void createLoanRequest() { working fine

\\  below method throws "Cannot flush EntityManager, an active transaction is require" error
   public LoanRequest createLoanRequest() {

      System.out.println("Calling JPA Entity");
      LoanRequest loanRequest = new LoanRequest();
      loanRequest.setId(3424L);
      loanRequest.setValue("kiran");
      entityManager.persist(loanRequest);
       return loanRequest;
      
   }
xml looks like:
  <serviceTask  id="loanDeptReq" name="loanDeptReq"
    activiti:expression="${loanService.createLoanRequest()}" 
    activiti:resultVariable="loanRequest"/>


pls let me know if some missing in config or code.

Regards
Kiran.
5 REPLIES 5

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
Just make sure Activiti and your entitymanager share the same transaction and one is started.

javatech
Champ in-the-making
Champ in-the-making
Hi Thxs it working.


Old config
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
  <property name="dataSource" ref="dataSource" />
  <property name="transactionManager" ref="transactionManager" />
  <property name="databaseSchemaUpdate" value="true" />
  <property name="jobExecutorActivate" value="false" />
  <property name="jpaEntityManagerFactory" ref="entityManagerFactory" />
  <property name="jpaHandleTransaction" value="true" />
  <property name="jpaCloseEntityManager" value="true" />
  <property name="jpaPersistenceUnitName" value="activiti-jpa-pu" />
 
</bean>

New cofig
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
  <property name="dataSource" ref="dataSource" />
  <property name="transactionManager" ref="transactionManager" />
  <property name="databaseSchemaUpdate" value="true" />
  <property name="jobExecutorActivate" value="false" />
  <property name="jpaEntityManagerFactory" ref="entityManagerFactory" />
  <property name="jpaHandleTransaction" value="true" />
  <property name="jpaCloseEntityManager" value="true" />
   
</bean>


Removed
<property name="jpaPersistenceUnitName" value="activiti-jpa-pu" />
from the config

Thxs

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

I get "Cannot flush EntityManager, an active transaction is required" 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 my service #{personService.persistPerson()}.  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 :
<code>
<?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/develorag" />
  <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>
</code>

my persistence.xml:
<code>
<?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>
</code>

and my process:
<code>

<?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" xmlnsSmiley Surprisedmgdc="http://www.omg.org/spec/DD/20100524/DC" xmlnsSmiley Surprisedmgdi="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>

</code>


and my service:
<java>

@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;
}
}
</java>

vasile_dirla
Star Contributor
Star Contributor
Hi,

As I know you should specify the unitName when you want to use a specific persistence unit (even it is the only one in your application)

You can try one of this 2 options:
1: removing
<code>
<property name="jpaPersistenceUnitName" value="myapp" />
</code>
This way the system will use the default name for it and @PersistenceContext (without parameters) will locate the default one
2: OR let the jpaPersistenceUnitName parameter but specify the unitName
<code>
@PersistenceContext(unitName="myapp")
</code>

this way you specify explicitly which persistence unit to use. (a long time ago i debugged that area and I remember that if there is no name specified will try to locate the default one (named "default" or something) and if is not detected the method will run in a NonTransactional context.

Anyway I ended up specifying the persistenceUnit name explicitly and it worked as expected.

ps:
I noticed this question was posted also separately, (please don't double post a question in the future because this way this forum will have a lot of duplicated items and is not nice)

Thanks.

hung
Champ in-the-making
Champ in-the-making
in your code you haven't got a transaction.
your configuration is using JBoss Transaction Manager, you can use userTransaction or container manager Transaction. Container Manager Transaction work only with EJB, ( transaction-type="RESOURCE_LOCAL" in persistence.xml should be removed , default is container)