users@glassfish.java.net

Re: Cannot use an EntityTransaction while using JTA.

From: Ryan de Laplante <ryan_at_ijws.com>
Date: Tue, 27 Mar 2007 17:53:20 -0400

Thank you!

So if I get my EntityManager instance by using Persistence.createEMF()
etc... then I will be able to use the EntityTransaction? I will try
this first thing in the morning. If this fails, then I will look into
moving my web service from a web app to an EJB session bean.


Ryan


Marina Vatkina wrote:
> In your case NetBeans is probably right. Injecting entity manager
> directly makes it a container-managed entity manager.
>
> See JPA spec sections:
> 5.5 Controlling Transactions
> ...
> A container-managed entity manager must be a JTA entity manager.
>
> 5.2.1 Obtaining an Entity Manager in the Java EE Environment
> A container-managed entity manager is obtained by the application
> through dependency injection, or direct lookup of the entity manager
> in the JNDI namespace.
>
> You can use UserTransactions to control JTA entity manager, but you
> need to call em.joinTransaction() after you started it.
>
> regards,
> -marina
>
> Ryan de Laplante wrote:
>> I don't know if NetBeans is buggy or not.. I don't understand what
>> I'm supposed to do to solve this problem.
>> I inject the entity manager like this:
>>
>> @PersistenceContext(unitName="MyPU")
>> private EntityManager em;
>>
>>
>> Thanks,
>> Ryan
>>
>>
>> Marina Vatkina wrote:
>>
>>> Hi Ryan,
>>>
>>> Removing transaction-type doesn't make a difference - it's JTA by
>>> default in a Java EE environment. If NetBeans doesn't work
>>> correctly, please file a bug with NetBeans directly.
>>>
>>> How do you get a hold of EM? Do you
>>> a) call Persistence.createEMF()
>>> b) inject or look up EMF
>>> c) inject or look up EM
>>>
>>> thanks,
>>> -marina
>>>
>>> Ryan de Laplante wrote:
>>>
>>>> Hi,
>>>>
>>>> I have a Java 5 web application who's only purpose is to provide a
>>>> web service. The web service uses JPA to read an entity from the
>>>> database, make a change and save it back into the db. Because this
>>>> is not an EJB, I have to manually begin and commit a transaction
>>>> for persist() to work.
>>>>
>>>> When I do the following, the app server throws an
>>>> IllegalStateException ("Cannot use an EntityTransaction while using
>>>> JTA.")
>>>>
>>>> EntityTransaction trans;
>>>> trans = em.getTransaction();
>>>>
>>>>
>>>> I opened my persistence.xml in NetBean's visual editor, unchecked
>>>> "Use Java Transaction APIs", then saved and redeployed. This did
>>>> not make a difference. I noticed a transaction-type="JTA"
>>>> attribute was still on the <persistence-unit> element so I removed
>>>> it, saved and redeployed. I still get an exception.
>>>>
>>>>
>>>> Thanks,
>>>> Ryan