@PersistenceUnit OR @PersistenceContext EntityManagerFactory
Hello
I am using JTA API for my application. Wanted to know a simple thing before starting the work. What is the better approach to obtaining a EntityManager in an EnterpriseBean.
Tech. no 1:
@PersistenceUnit
EntityManagerFactory emf;
EntityManager = emf.createEntityManager();
OR
Tech. no 2:
@PersistenceContext
EntityManager em;
Thanks
[412 byte] By [
rabbiaa] at [2007-11-26 17:14:20]

# 1
@PersistenceContext is better.Using @PersistenceContext produces what is
referred to as a "container-managed" EntityManager. That means the container
does work on your behalf like automatically propagating the persistence context
when it's used in a transaction. It's similar to the distinction between
container-managed transactions and bean-managed transactions.
The alternative is referred to as an "application-managed" EntityManager. Using
that approach gives you finer-grained control over the lifecycle of the EntityManager
and its association with a global transaction, but comes at the cost of more
complex application code.
--ken