users@jersey.java.net

Cannot use an EntityTransaction while using JTA, cannot switch to application managed transactions

From: Jacob <skoczko_at_gmail.com>
Date: Tue, 8 Apr 2008 10:43:36 +0200

Hi All,

I am using Netbeans to develop RESTful web services (using JAX-RS) on
top of entity beans anotated in JPA and persisted in MySQL. I tried
the built-in netbeans wizard to generate restful ws from entity beans.
However, when I try to access the web service I get a following
exception:

java.lang.IllegalStateException:
Exception Description: Cannot use an EntityTransaction while using JTA.



Indeed I'm using JTA in my persistence.xml, does it automatically mean
that I cannot use EntitytTransaction? If so, how do I persist and
merge objects? I cannot do it without a transaction because I'm
getting:

javax.persistence.TransactionRequiredException



I tried switching to application-managed transactions (and using the
UserTransaction instead of EntityTransaction) but I cannot get the
persistence unit to work. If I substitute the auto-genereated thread
local based PersistenceService and annotate my class with the
following:

@PersistenceUnit(unitName = "localindexes")
private EntityManagerFactory emf;

private EntityManager getEntityManager() {
  return emf.createEntityManager();
}

@Resource
private UserTransaction utx;


I get yet another exception:

javax.servlet.ServletException:
com.sun.ws.rest.api.container.ContainerException: Persistence unit
'localindexes' is not configured as a servlet parameter in web.xml


Putting:

<persistence-context-ref>
      <description>Persistence context for the web services</description>
      <persistence-context-ref-name>persistence/localindexes</persistence-context-ref-name>
      <persistence-unit-name>localindexes</persistence-unit-name>
  </persistence-context-ref>



in the web.xml or annotating the ws class with
@PersistenceContext(name = "persistence/localindexes", unitName =
"localindexes") does not help at all.

I'm already tearing my hair out so any help would be appreciated.
Thanks in advance.

my persistence.xml uses toplink as a persistence provider:

<persistence-unit name="localindexes" transaction-type="JTA">
      <provider>oracle.toplink.essentials.PersistenceProvider</provider>
      <jta-data-source>mysql-localhost</jta-data-source>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
          <property name="toplink.ddl-generation" value="create-tables"/>
      </properties>
  </persistence-unit>