Hi Mike, Gordon,
What is the rational behind allowing EMF.getEntityManager() to return an
EntityManager even though there is no active JTA transaction?
In my code, if I do the following, what is the expected behavior:
EntityManager em = emf.getEntityManager();
UserCredential credential = em.find(UserCredential.class, name);
credential.setPassword("newpassword");
utx.begin();
EntityManager em2 = emf.getEntityManager();
// Should em and em2 share the same PersistenceContext? I am
currently observing different behavior in glassfish.
UserCredential credential2 = em2.find(UserCredential.class,
name);
// I am expecting to see changed password in credential2,
but I don't see this.
If I begin the transaction first and then subsequent EntityManagers
returned by getEntityManager() share the same persistence context. But
in my code, I first called getEntityManager(), then utx.begin() and then
again getEntityManager(). So I am not sure, what I am observing is a bug
or indeed the correct behavior. If this is the correct behavior, do you
think average programmers will understand these subtle differences?
The javadoc for getEntityManager() says the following:
/**
* Get an EntityManager instance whose persistence context
* is propagated with the current JTA transaction.
* If there is no persistence context bound to the current
* JTA transaction, a new transaction-scoped persistence
* context is created and associated with the transaction
* and the entity manager instance that is created and
* returned. If no JTA transaction is in progress, an
* EntityManager instance is created for which the persistence
* context will be propagated with subsequent JTA transactions.
* Throws IllegalStateException if called on an
* EntityManagerFactory that does not provide JTA EntityManagers.
*/
EntityManager getEntityManager();
Thanks,
Sahoo