About Question enthuware.oce-jpad.v6.2.518 :

Moderator: admin

Post Reply
cosminvacaroiu

About Question enthuware.oce-jpad.v6.2.518 :

Post by cosminvacaroiu » Sun Sep 02, 2012 8:48 am

Isn't a

Code: Select all

em.joinTransaction()
required here ? otherwise the persist will do nothing

admin
Site Admin
Posts: 8245
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.518 :

Post by admin » Sun Sep 09, 2012 3:19 pm

No, it is not required here because a new transaction is being created in the same code. joinTransaction is required when the transaction is created else where.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.518 :

Post by himaiMinh » Sat Aug 12, 2017 5:08 pm

I have the same question too. The entity manager is not created before em.getTransaction().begin which creates the transaction.
Why we don't need to call em.joinTransaction?
I assume the code is like this:

Code: Select all

EntityManagerFactory emf = Persistence.createEntityManagerFactory("aPU");
  EntityManager em = emf.createEntityManager();

  //This persistence context is created when the em is created.
  // It is created outside the new transaction 

   //The new transaction is created and begin after the em is created.
   em.getTransaction().begin();

The JPA Pro says "If the persistence context was created earlier(outside of a transaction or in a transaction that has since ended), the persistence context can be manually synchronized with the transaction by calling joinTransaction() on the EntityManager interface."
I guess the above statement is only applicable to the application managed entity manager used with JTA.
For example:

Code: Select all

//In a stateful  bean
@PersistenceContext(unitName="aPU")
EntityManagerFactory emf;
EntityManager em;
public void init(){
 emf = Persistence.createEntityManagerFactory("aPU");
 em = emf.createEntityManager();
}

public void add(....){
   em.joinTranscation();
   //anEntity is an entity
   em.persist(anEntity);
}

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests