This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 132828 - [mysql/rest] - Error - Cannot use an EntityTransaction while using JTA
Summary: [mysql/rest] - Error - Cannot use an EntityTransaction while using JTA
Status: RESOLVED FIXED
Alias: None
Product: webservices
Classification: Unclassified
Component: REST (show other bugs)
Version: 6.x
Hardware: All All
: P2 blocker (vote)
Assignee: Peter Liu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-04-15 01:34 UTC by Ayub Khan
Modified: 2008-06-20 18:07 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ayub Khan 2008-04-15 01:34:59 UTC
On behalf of External User (Jacob <skoczko@gmail.com>)
==========================
Subject: Cannot use an EntityTransaction while using JTA, cannot switch to application ma


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>

=====================
Comment 1 Ayub Khan 2008-04-15 01:36:02 UTC
Workaround:

Temporary solution I found is to look-up the EntityManager and
UserTransaction in the container:

    private EntityManager getEntityManager() {
        EntityManager em = null;
        try {
            em = (EntityManager) new
InitialContext().lookup("java:comp/env/persistence/localindexes");
        } catch (NamingException e) {
            Logger.getLogger(HarvestablesResource.class.getName()).log(Level.SEVERE,
null, e);
        }
        return em;
    }

    private UserTransaction getUserTransaction() {
        UserTransaction utx = null;
        try {
            utx = (UserTransaction) new
InitialContext().lookup("java:comp/UserTransaction");
        } catch (NamingException e) {
            Logger.getLogger(HarvestablesResource.class.getName()).log(Level.SEVERE,
null, e);
        }
        return utx;
    }

However, I would still very much like to get the annotations to work.
Comment 2 Peter Liu 2008-06-20 00:02:10 UTC
Starting with NB 6.1 milestone 1, if you are running against a container such as glassfish which supports JTA, the
generated CRUD app will use JNDI lookup to look up an instance of UserTransaction and use it for transaction processing
. If you are running against Tomcat without any JTA support installed, the generated CRUD app will continue to use
EntityTransaction.

Comment 3 Lukas Jungmann 2008-06-20 10:52:12 UTC
TM says 6.0M1, your (Peter's) comment says 6.1M1 and I saw some change(s)[1] perhaps related to this which will become
visible in 6.5M1... So what's the correct TM here?
Comment 4 Lukas Jungmann 2008-06-20 10:52:59 UTC
forgotten reference: [1]: http://hg.netbeans.org/main?cmd=changeset;node=8792e429b881
Comment 5 Peter Liu 2008-06-20 18:07:21 UTC
Oops, I meant to select 6.5m1.