NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

EE5 JPA with JTA/UserTransaction -- non-Container managed transactions

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
emiddio-frontier
Posted via mailing list.





PostPosted: Mon Dec 13, 2010 1:12 am    Post subject: EE5 JPA with JTA/UserTransaction -- non-Container managed transactions Reply with 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
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo