title: OpenJPA - Transaction management is not available... (Fuse ESB)
body: <p>I'm having trouble with <code>RESOURCE_LOCAL</code> transaction type for JPA in Fuse ESB.</p>
  
  <p>I also don't have a complete understanding of whether <code>JTA</code> or <code>RESOURCE_LOCAL</code> is better for me.</p>
  
  <p>My persistence.xml :</p>
  
  <pre><code>&lt;persistence-unit name="invoicePersistence" transaction-type="RESOURCE_LOCAL"&gt;
  
      &lt;provider&gt;org.apache.openjpa.persistence.PersistenceProviderImpl&lt;/provider&gt;
      &lt;jta-data-source&gt;osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/invDataSource)&lt;/jta-data-source&gt;
      &lt;non-jta-data-source&gt;osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/invDataSource)&lt;/non-jta-data-source&gt;
      &lt;class&gt;com.company.service.Invoice&lt;/class&gt;
      &lt;!-- etc... --&gt;
  
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>My beans in blueprint.xml :</p>
  
  <pre><code>&lt;reference id="invDataSource" interface="javax.sql.DataSource" filter="(datasource.name=invDataSource)"/&gt;
  
  &lt;bean id="invoiceDao" class="com.company.project.InvoiceDao"&gt;
      &lt;jpa:context unitname="invoicePersistence" property="entityManager"/&gt;
      &lt;tx:transaction method="*" value="Required" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>And my code :</p>
  
  <pre><code>    entityManager.getTransaction().begin();
  
      entityManager.persist(a);
      entityManager.persist(b);
  
      entityManager.getTransaction().commit();
  </code></pre>
  
  <p>And the exception, using transaction-type <code>RESOURCE_LOCAL</code> in my persistence.xml:</p>
  
  <pre><code>java.lang.IllegalStateException: Transaction management is not available for container managed EntityManagers.
  </code></pre>
  
  <p>I also tried using transaction-type <code>JTA</code> in my persistence.xml but then I received <code>OptimisticLockException</code>.</p>
  
  <p>I'm not sure which approach is better (RESOURCE_LOCAL or JTA) but the main thing is that in my code object <code>a</code> and <code>b</code> need to be persisted in a transaction (all or nothing).</p>
  
  <p>I'm running in Fuse ESB (camel, cxf, etc).</p>
  
  <p>Thanks for any tips or help.</p>