NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
emiddio-frontier Posted via mailing list.
|
Posted: Mon Dec 13, 2010 4:18 am Post subject: [SPAM] Re: EE5 JPA with JTA/UserTransaction -- non-Container managed transactions |
|
|
so how to do it with JNDI --i have not seen the way to do it ??? what is the
name to lookup???
my persistence.xml says:
<persistence-unit name="EmployeeService" transaction-type="JTA" >
...
and the API for javax.persistence.Persistence says:
public class Persistenceextends java.lang.ObjectBootstrap class that is used
to obtain an EntityManagerFactory in Java SE environments.
The Persistence class is available in a Java EE container environment as
well; however, support for the Java SE bootstrapping APIs is not required in
container environments.
so it is supposed to work in java EE also.
gary
----- Original Message -----
From: "Sahoo" <address-removed>
To: <address-removed>
Cc: "emiddio-frontier" <address-removed>; <address-removed>;
<address-removed>
Sent: Sunday, December 12, 2010 6:52 PM
Subject: Re: EE5 JPA with JTA/UserTransaction -- non-Container managed
transactions
| Quote: | Persistence.createEMF()
is not same as
@PersistenceUnit
EMF emf;
If you don't like injection, you can look up emf using JNDI and that will
give you same behavior, but Persistence.creatEMF is called "Java SE" style
of obtaining an EMF and that's not recommended to be used in EE apps, for
in Java SE, things like JTA may not be supported by the EMF. In fact,
unless you explicitly configure transaction-type in your persistence.xml,
the default value is RESOURCE_LOCAL for Java SE and JTA for Java EE.
I am quite sure the spec says all these things.
Sahoo
On Monday 13 December 2010 06:31 AM, emiddio-frontier wrote:
| Quote: | Why does code like:
emf =
javax.persistence.Persistence.createEntityManagerFactory("EmployeeService");
provide an emf that will not work properly?
see more info below:
Rather than code like:
@PersistenceUnit(unitName = "EmployeeService")
public EntityManagerFactory emf;
@Resource
public UserTransaction utx;
the JavaEE API says one can do the following:
public EntityManagerFactory emf;
public UserTransaction utx;
and initialize emf and utx like:
InitialContext ic = new InitialContext();
utx = (UserTransaction) ic.lookup("java:comp/UserTransaction");
emf =
javax.persistence.Persistence.createEntityManagerFactory("EmployeeService");
-----------------------------------------------------------------------
Within a Servlet with JTA/JPA, code like the shown below works as long as
emf
is only obtained with/via the @PersistenceUnit annotation;
EntityManager em = emf.createEntityManager();
utx.begin();
em.joinTransaction();
Employee emp = em.find(Employee.class, id);
utx.commit();
the above code works if utx is obtained as a
@Resource
public UserTransaction utx;
or as
utx = (UserTransaction) ic.lookup("java:comp/UserTransaction");
the code fails if emf is obtained as
emf =
javax.persistence.Persistence.createEntityManagerFactory("EmployeeService");
as opposed to
@PersistenceUnit(unitName = "EmployeeService")
public EntityManagerFactory emf;
the Java EE5/6 API say it should work.
thanks
gary
|
|
|
|
| Back to top |
|
 |
emiddio-frontier Posted via mailing list.
|
Posted: Mon Dec 13, 2010 7:13 am Post subject: [SPAM] Re: EE5 JPA with JTA/UserTransaction -- non-Container managed transactions |
|
|
thanks -- sounds like i need to study the EE[56] Spec.
gary
----- Original Message -----
From: "Sahoo" <address-removed>
To: <address-removed>
Cc: "emiddio-frontier" <address-removed>; <address-removed>;
<address-removed>
Sent: Sunday, December 12, 2010 8:49 PM
Subject: Re: EE5 JPA with JTA/UserTransaction -- non-Container managed
transactions
| Quote: |
On Monday 13 December 2010 09:36 AM, emiddio-frontier wrote:
| Quote: | so how to do it with JNDI --i have not seen the way to do it ??? what is
the
name to lookup???
my persistence.xml says:
<persistence-unit name="EmployeeService" transaction-type="JTA" >
...
| You need to define a <persistence-unit-ref> entry in your EE deployment
descriptor and then you will be able to look up. See chapter 5 of EE
platform spec or do some search in the net.
| Quote: |
and the API for javax.persistence.Persistence says:
public class Persistenceextends java.lang.ObjectBootstrap class that is
used to obtain an EntityManagerFactory in Java SE environments.
The Persistence class is available in a Java EE container environment as
well; however, support for the Java SE bootstrapping APIs is not required
in container environments.
so it is supposed to work in java EE also.
| Well, who said it is not supposed to work. If you read the spec entirely,
you will see the created EMF behaves differently than the injected one.
Sahoo |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|